11.9. Structure of Classes and Objects in Java: Abstract Classes

In the world of object-oriented programming (OOP), Java stands out as a language that rigorously implements this paradigm. One of the fundamental concepts of OOP is the structure of classes and objects, and within this context, abstract classes play an essential role. In this chapter, we will explore what abstract classes are, how and when to use them in Java.

What are Abstract Classes?

Abstract classes in Java are a special type of class that cannot be instantiated directly. They are declared with the abstract keyword and are mainly used as base classes for other classes. An abstract class can contain abstract methods, which are methods without a body, that is, without implementation. The idea is that concrete subclasses that inherit from an abstract class provide specific implementations for those abstract methods.

Why use Abstract Classes?

Abstract classes are useful when you have a general idea of ​​a feature, but the exact implementation can vary depending on the class that extends the abstract class. They allow you to define a contract for subclasses, forcing them to implement specific abstract methods. Additionally, abstract classes can contain method implementations that can be shared by all subclasses, avoiding code duplication and promoting reuse.

How to Define an Abstract Class


abstract class Vehicle {
    private String tag;

    public Vehicle(String brand) {
        this.brand = brand;
    }

    public String getMarca() {
        return mark;
    }

    // Abstract method
    public abstract void move();
}

In the example above, Vehicle is an abstract class that contains an abstract method move(). This method has no body and therefore has no implementation in the Vehicle class. Any class that extends Vehicle must provide its own implementation of move().

Implementing Concrete Classes from Abstract Classes


class Car extends Vehicle {
    public Car(String brand) {
        super(brand);
    }

    // Abstract method implementation
    public void move() {
        System.out.println("The car is moving.");
    }
}

In the example above, Car is a concrete class that extends the abstract class Vehicle. It provides the implementation of the abstract method move(), which is mandatory.

Characteristics of Abstract Classes

  • Abstract classes cannot be instantiated directly.
  • Can contain abstract methods and concrete methods.
  • Abstract methods have no body and must be implemented by subclasses.
  • Abstract classes can have data members, constructors, and static blocks.
  • An abstract class can have constructors, but they cannot be used to create instances directly.

When to use Abstract Classes

Abstract classes are ideal when you have a situation where several classes share a common structure or methods, but each of them needs different implementations for some behaviors. They are an excellent tool for representing generic and base concepts, leaving the specificity to the subclasses.

Practical example

Imagine that you are developing a system to manage a fleet of vehicles. You can have multiple categories of vehicles such as cars, motorcycles and trucks, each with its own way of moving. However, they all share common characteristics such as make and model. An abstract Vehicle class can provide the base structure with common members and methods, while specific subclasses implement the details of how each type of vehicle moves.

Final Considerations

Abstract classes are a fundamental part of object-oriented programming in Java. They allow you to create a solid and flexible foundation for your code, promoting reuse and extensibility. When designing your system, think carefully about which concepts are generic enough to become abstract classes and which are specific enough to justify concrete subclasses.

Understanding and correctly applying abstract classes is an important step towards becoming an advanced Java developer. By mastering this concept, you will be well equipped to create robust, maintainable systems that take advantage of the power of inheritance and polymorphism in Java.

Now answer the exercise about the content:

_Which of the following statements about abstract classes in Java is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Structure of classes and objects in Java: Static Members

Next page of the Free Ebook:

68Structure of classes and objects in Java: Static Members

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text