terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat oop-abstraction.md
guest@codeandosimple: ~/blog/oop $ cat abstraction.md

Abstraction_

// "The price of success is hard work" - Vince Lombardi

Abstraction (abstract classes being one of its representatives) is a fundamental principle in object-oriented programming, which allows developers to focus on what an object does without having to deal with the details of how it does it.

In this chapter, we will see the concept of abstraction, how it is implemented through abstract classes and methods, and provide guidance on why and when to use it.

# What is Abstraction?

Abstraction is the process of hiding complex implementation details and showing only the essential functionalities to the user.

It focuses on what an object does, not how it does it, allowing developers to work with concepts at a more general and intuitive level.

# Abstract Methods

An abstract method is a method that is declared without a body.

It serves as a "contract" that guarantees that subclasses will have to implement this method, a specific implementation for each subclass.

# UML

In UML, an abstract method is written in italics, indicating that it must be redefined. The name of the class is also italicized. As in this case, the draw() method and the name of the Shape class.

abstraction and abstract classes

# Abstract Classes

An abstract class is one that cannot be instantiated; it must be extended by another class that implements its abstract methods.

An abstract class is any class that has at least one abstract method.

It can have attributes and concrete (implemented) methods, but if it has one or more abstract ones, it is abstract.

Characteristics

  • Non-Instantiable: You cannot create an object directly from an abstract class.

  • Can Contain Abstract Methods: An abstract method is a method that is declared without an implementation. Subclasses must provide a concrete implementation for these methods.

  • Can Contain Concrete Methods: Abstract classes can also have methods with full implementation, which allows for some degree of code reuse.

public abstract class Vehicle {
  public abstract void move();
}

In this example, Vehicle is an abstract class; it has at least one abstract method.

Any class that extends Vehicle must provide its own version of the move method.

# Why Use Abstraction?

  • Simplicity: Abstraction allows developers to work with operations at a high level, without worrying about the complex details of their implementation.

  • Code Reuse: Abstract classes can contain a partial implementation that will be shared by all their subclasses, reducing code duplication.

  • Extensibility: By defining a "contract" through abstract methods, abstract classes make code easier to extend and maintain.

# When to Use Abstraction?

  • When you have related classes that must share common code but also have their own behaviors.

  • When you want to hide complex implementation details, so users of the class focus on what a method does and not how it does it.

# Practical Example: Geometric Shapes System

As an example of abstraction, let's consider a geometric shapes system.

We can define an abstract class Shape that contains abstract methods to calculate area and perimeter. We will create specific implementations for different types of shapes.

public abstract class Shape {
  public abstract double area();
  public abstract double perimeter();
}

Concrete classes:

public class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }
}

public class Rectangle extends Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double area() {
        return length * width;
    }

    @Override
    public double perimeter() {
        return 2 * (length + width);
    }
}

# Conclusions

Abstraction is a powerful technique that helps simplify software design by allowing developers to focus on high-level operations.

By using abstract classes and abstract methods, one can create a clear and flexible hierarchy that promotes code reuse and facilitates extensibility and maintenance.

Understanding and applying abstraction will allow you to build more robust and scalable systems; good practices and design patterns are based on abstractions.