13.8 Inheritance and Polymorphism in Java: Concept of Polymorphism
The concept of polymorphism is one of the fundamental pillars of object-oriented programming (OOP), and Java, being a language that adopts this paradigm, offers full support for this characteristic. The term polymorphism comes from the Greek "polys", which means many, and "morphē", which means form. In programming, this refers to the ability of a variable, function, or object to take on various forms.
Polymorphism in Java
In Java, polymorphism manifests itself mainly in two ways: overloading polymorphism and overriding. The first refers to the ability to have multiple functions with the same name but different parameter lists within the same class. The second, which is the focus of this text, is related to inheritance and allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes or superclasses.
Inheritance and Polymorphism
Inheritance allows a new class, known as a subclass, to inherit fields and methods from another class, called a superclass. Substitution polymorphism comes into play when a subclass redefines a method of the superclass. This allows the subclass to specialize or modify the behavior of the inherited method. When a subclass object is used, the redefined method is what will be called, even if the reference is of the superclass type.
Example of Polymorphism
class Animal {
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("The cat meows");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.sound(); // Output: The animal makes a sound
myDog.sound(); // Output: The dog barks
myCat.sound(); // Output: The cat meows
}
}
In the example above, the class Animal
has a method sound()
that is redefined (overridden) in the classes Dog
and Cat
. Although the myCachorro
and myCat
references are of type Animal
, the methods called are those of the objects' actual classes, that is, Dog
and Gato
, respectively. This is polymorphism in action.
Benefits of Polymorphism
Polymorphism brings several benefits to software development, including:
- Flexibility: Allows you to write code that can work with objects of different types and classes.
- Code reuse: The same code can be used to interact with objects from different classes that are derived from the same superclass.
- Maintainability: Facilitates code maintenance and updating, since changes to a superclass can be automatically inherited by subclasses.
Interfaces and Polymorphism
In addition to class inheritance, polymorphism in Java can also be achieved through interfaces. An interface defines a contract that concrete classes must follow, specifying methods that must be implemented. Since a class implements an interface, any object of that class can be referred to as being of the interface's type. This allows different classes that implement the same interface to be treated polymorphically.
Animal interface {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("The dog barks");
}
}
class Cat implements Animal {
@Override
public void sound() {
System.out.println("The cat meows");
}
}
public class TestInterfacePolymorphism {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.sound(); // Output: The dog barks
myCat.sound(); // Output: The cat meows
}
}
In the example above, Animal
is an interface, and the classes Dog
and Cat
implement this interface. This allows objects of both classes to be referenced as objects of type Animal
and the code can interact with them in a polymorphic way.
Final Considerations
Polymorphism is a powerful concept that allows the construction of flexible and easily extensible systems. In Java, implementing polymorphism is straightforward and natural, thanks to its rob support.Use of object-oriented programming. Understanding and correctly applying polymorphism is essential for any Java developer who wants to create reusable, maintainable, and scalable code.