13.7 Inheritance and Polymorphism in Java: Abstract Classes

Object-oriented programming (OOP) is a programming paradigm that uses "objects" to model data and behavior. Java, being an object-oriented language, fully supports concepts such as inheritance and polymorphism, which are fundamental for reusing code and improving the maintenance of complex systems. In this chapter, we will explore the concept of inheritance and polymorphism in Java, with a special focus on abstract classes.

Inheritance in Java

Inheritance is a mechanism by which a new class, called a subclass, can inherit properties and methods from another class, called a superclass. The subclass can add new fields and methods or override inherited methods. The main advantage of inheritance is the ability to reuse existing code, which makes software easier to develop and maintain.

In Java, inheritance is performed using the extends keyword. For example:


class Animal {
    void eat() {
        System.out.println("This animal eats.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

The Dog class inherits the eat() method from the Animal class, but also defines its own method, bark() .

Polymorphism in Java

Polymorphism is the ability of an object to be referenced in multiple ways. In Java, this is mainly achieved through the use of inheritance and interfaces. Polymorphism allows objects from different subclasses to be treated as objects from a superclass. This is useful in many scenarios, such as when we want to write code that can work with objects of different classes without knowing exactly which class they are.

For example, we can have a method that accepts an object of class Animal and calls the eat() method on it. This method will work with any object that is an instance of Animal or any subclass of Animal, such as Dog, without needing to know the exact type of the object.

Abstract Classes in Java

Abstract classes are classes that cannot be instantiated by themselves and are designed to be subclasses. They are declared with the abstract keyword. Abstract classes are often used to create a base class with common methods and properties that will be shared by all subclasses, but leaving the implementation of one or more methods to the subclasses.

For example:


abstract class Shape {
    int x, y;

    void move(int dx, int dy) {
        x += dx;
        y += dy;
    }

    abstract void draw();
    abstract double area();
}

class Circle extends Shape {
    double radius;

    void draw() {
        System.out.println("Draw circle.");
    }

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

The Forma class is abstract and cannot be instantiated directly. It defines a concrete method, move(), and two abstract methods, draw() and area(), which must be implemented by subclasses . The Circle class is a concrete subclass of Shape and provides implementations for the abstract methods.

Benefits of Abstract Classes

Abstract classes are very useful when we want to define a template for a group of subclasses. With abstract classes, we can:

  • Define methods that must be implemented by subclasses, ensuring a consistent interface.
  • Implement non-abstract methods that can be shared or reused by subclasses.
  • Define data fields that can be used by subclasses.
  • Control how subclass objects should be created, using constructors in the abstract class.

Abstract classes are a powerful tool in a Java developer's arsenal. They provide a way to establish a clear relationship between related classes, while still taking advantage of the benefits of inheritance and polymorphism. By using abstract classes, we can create more flexible and maintainable systems, which is a significant advantage in software projects of any size.

In summary, inheritance and polymorphism are fundamental concepts in Java that allow us to write reusable code and more flexible systems. Abstract classes play a crucial role in this, allowing us to define class structures that are both robust and extensible. By mastering these concepts, Java developers can create powerful, efficient applications that are easy to maintain and expand over time.

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 Inheritance and Polymorphism in Java: Concept of Polymorphism

Next page of the Free Ebook:

92Inheritance and Polymorphism in Java: Concept of Polymorphism

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