13.2 Inheritance and Polymorphism in Java: 'extends' keyword

Inheritance is one of the fundamental pillars of object-oriented programming (OOP). In Java, inheritance allows a class to inherit fields and methods from another class, promoting code reuse and the creation of a hierarchical relationship between classes. The extends keyword is used to establish this inheritance relationship between two classes.

Concept of Inheritance

In inheritance, we have two main entities: the parent class (or superclass) and the child class (or subclass). The child class inherits the attributes and behaviors of the parent class, but can also define its own members and behaviors. This means that the child class is a specialization of the parent class, where it not only has the traits of the parent class but also adds its own unique features.

Using the 'extends' keyword

To implement inheritance in Java, we use the extends keyword in the child class declaration. Let's look at a simple example:


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

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

In the example above, Dog is a subclass of Animal. This means that Dog inherits the eat() method from Animal and also defines its own bark() method. An object of class Dog can call both methods.

Polymorphism and Inheritance

Polymorphism is another key concept in OOP that is closely related to inheritance. It means "many forms" and allows objects to be treated as instances of their parent classes rather than their actual classes. In Java, this is possible thanks to inheritance and the fact that a superclass reference variable can point to a subclass object. This allows methods to be called polymorphically.

For example:


Animal myAnimal = new Dog();
myAnimal.eat(); // Call the Animal class method
// myAnimal.bark(); // Compilation error: Animal type does not have the bark method

Here, the variable myAnimal is of type Animal, but points to an instance of Dog. This allows the eat() method to be called polymorphically. However, if we try to call bark() through myAnimal, we will receive a compilation error, as the compiler only knows the methods defined in the Animal.

Method Overriding

An important feature of inheritance in Java is the ability to override methods. This means that a subclass can provide its own implementation of a method that is already defined in its superclass. To override a method, the subclass must define a method with the same signature as the superclass's method.


class Cat extends Animal {
    @Override
    void eat() {
        System.out.println("The cat eats fish");
    }
}

In the example above, the Cat class overrides the eat() method of the Animal class. The @Override annotation is optional, but it is good practice to use it because it tells the compiler and developers that the method is an intentional override.

Polymorphism and Method Overriding

When a method is overridden, polymorphism ensures that the overridden version of the method is called when the object is treated as an instance of the superclass. This is a fundamental part of what makes polymorphism so powerful in Java.


Animal myCat = new Cat();
myCat.eat(); // Call the overridden method of the Cat class

In this case, even if myCat is of type Animal, the method eat() that is called is that of the class Gato, because the real object is an instance of Gato.

Final Considerations

Inheritance and polymorphism are fundamental concepts that allow Java classes to be reusable, extensible, and manageable. The extends keyword is the gateway to inheritance, while polymorphism allows objects of different classes to be treated uniformly. Understanding these concepts is essential for any programmer who wants to master Java and develop robust and flexible systems.

In addition, it is important to be aware of the limitations and considerations when using inheritance, such as the "diamond" problem in multiple inheritance (which Java solves by not allowing multiple inheritance of classes) and the importance of encapsulation to maintain a hierarchy of well-defined and safe classes.

In short, ivynce and polymorphism are powerful tools that, when used correctly, can significantly improve software design and maintenance. The extends keyword is just the beginning of a vast world of possibilities that object-oriented programming offers.

Now answer the exercise about the content:

Which of the following statements about inheritance and polymorphism 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: Method Overriding

Next page of the Free Ebook:

87Inheritance and Polymorphism in Java: Method Overriding

4 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