Inheritance is one of the fundamental concepts of object-oriented programming (OOP) and Java is a language that fully incorporates these concepts. Inheritance allows a class to acquire properties and behaviors from another class, promoting code reuse and the creation of a class hierarchy. In Java, syntax and concepts related to inheritance are central to creating efficient, well-structured applications.

Concept of Inheritance

In Java, inheritance is used to establish an "is-a" relationship between classes. If a class B is derived from a class A, we say that B is a specialized type of A. For example, if we have a class "Vehicle" and a class "Car", we can say that "Car" is a "Vehicle", therefore, "Car" inherits from "Vehicle".

This means that "Car" inherits all of the public and protected properties and methods of the "Vehicle" class, unless "Car" overrides them (a concept known as overriding). Additionally, "Car" can add its own specific methods and properties.

Inheritance Syntax in Java

To implement inheritance in Java, we use the extends keyword. Here is a simple example of how inheritance can be expressed:

class Vehicle {
    // Properties and methods of the Vehicle class
}

class Car extends Vehicle {
    // Additional properties and methods of the Car class
}

In the example above, "Car" inherits from "Vehicle". This means that "Car" has access to all the methods and properties of "Vehicle" (except the private ones), and can also have its own methods and properties.

Using the Superclass Constructor

When a class inherits from another, the constructor of the superclass (the class from which it is inheriting) is not inherited. To call the superclass constructor, we use the super keyword. This is usually done on the first line of the subclass constructor:

class Vehicle {
    Vehicle() {
        // Superclass constructor
    }
}

class Car extends Vehicle {
    Car() {
        super(); // Call the constructor of the Vehicle class
        // Additional code for the Car builder
    }
}

Using super is essential when the superclass does not have a default constructor (without parameters) or when we want to call a superclass-specific constructor.

Method Overriding

In Java, a subclass can override the methods of the superclass. This is done by declaring a method in the subclass with the same signature as the method in the superclass. Overriding allows the subclass to provide a specific implementation of a method that already exists in the superclass.

class Vehicle {
    void move() {
        System.out.println("Vehicle is moving");
    }
}

class Car extends Vehicle {
    @Override // Optional annotation indicating method override
    void move() {
        System.out.println("Car is moving");
    }
}

In the example above, the move() method in the "Car" class overrides the move() method in the "Vehicle" class. The @Override annotation is optional, but it is good practice to use it because it helps identify compile-time errors if the method does not correctly correspond to a method in the superclass.

Polymorphism with Inheritance

Inheritance and polymorphism go hand in hand in Java. Polymorphism is the ability to treat objects from different subclasses of the same superclass as if they were the same type. This allows methods to be written in a more generic way, dealing with the superclass, but working correctly with any subclass.

Vehicle myVehicle = new Car();
myVehicle.move(); // Call the move() method of Car

In the example above, even if myVehicle is of type "Vehicle", the move() method that is called is that of the "Car" class, because the object real is an instance of "Car".

Access to Superclass Members

When a subclass overrides a superclass member, it is sometimes necessary to access the superclass member. This is done using the super keyword.

class Car extends Vehicle {
    @Override
    void move() {
        super.move(); // Calls the move() method of the Vehicle superclass
        // Additional code specific to Car
    }
}

In the above example, super.mover() is used to call the move() method of the "Vehicle" superclass before executing additional code from the "Car" subclass ".

Inheritance Constraints

Although inheritance is powerful, there are some restrictions in Java:

  • A class can only inherit from a single superclass (Java does not support multiple inheritance directly).
  • Classes marked withfinal cannot be inherited.
  • Methods marked as final cannot be overwritten.

Conclusion

Inheritance is a cornerstone of OOP and provides a powerful way to organize and reuse code. The syntax of inheritance in Java is straightforward, but it is important to understand the details and best practices to use inheritance effectively. By creating a class hierarchy with inheritance, we can build more modular and easier to maintain applications.

Now answer the exercise about the content:

_Which of the following statements about inheritance in Java is correct?

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

You missed! Try again.

Article image Java Basic Syntax: Polymorphism

Next page of the Free Ebook:

15Java Basic Syntax: 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