4.11. Java Basic Syntax: Polymorphism
Polymorphism is one of the four fundamental pillars of Object-Oriented Programming (OOP), along with encapsulation, inheritance and abstraction. In Java, polymorphism allows an object to be referenced in multiple ways, which is crucial for code reuse and creating flexible, extensible systems. In this chapter, we'll explore the concept of polymorphism, how it manifests itself in Java, and how you can use it to improve your programs.
What is Polymorphism?
Polymorphism, from the Greek "polys" (many) and "morphē" (form), refers to the ability of a method, object or variable to be represented in several ways. In the context of Java, this generally means that a reference from a parent class can point to an object from a child class, allowing methods to be invoked in such a way that the specific implementation of the method is determined at run time (runtime polymorphism ).
Types of Polymorphism in Java
There are two main types of polymorphism in Java:
- Compilation (or Static) Polymorphism: Also known as method overloading, it occurs when two or more methods in the same class have the same name, but different parameters. The decision about which method will be called is made at compile time.
- Runtime (or Dynamic) Polymorphism: Also known as method overriding, it occurs when a child class provides a specific implementation of a method that is already provided by its class parent or interface. The decision about which method implementation will be executed is made at run time.
Runtime Polymorphism
To better understand runtime polymorphism, consider the following example:
class Animal {
void talk() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
void talk() {
System.out.println("The dog barks: Woof!");
}
}
class Cat extends Animal {
void talk() {
System.out.println("The cat meows: Meow!");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.talk();
myDog.talk();
myCat.talk();
}
}
In this example, the class Animal
has a method talk()
, which is overridden by the classes Dog
and Cat
code>. In the main
method, we create three references of type Animal
, but two of them point to objects of specific subclasses. When the talk()
method is invoked on the meuCachorro
and meuCat
references, the specific implementations of Dog
and Cat
are called, respectively. This demonstrates runtime polymorphism.
Benefits of Polymorphism
Polymorphism offers several benefits, including:
- Flexibility: Allows programs to call methods that they do not necessarily know how they are implemented at compile time.
- Code reuse: With polymorphism, it is possible to write code that works with classes that have not yet been created.
- Maintainability: Polymorphism helps you create code that is easier to understand and maintain.
Interfaces and Polymorphism
Interfaces in Java are another mechanism that promotes polymorphism. An interface is a contract that specifies which methods a class should implement, without providing the implementation itself. When a class implements an interface, any type reference of that interface can point to an object of that class.
Animal interface {
void speak();
}
class Dog implements Animal {
public void speak() {
System.out.println("The dog barks: Woof!");
}
}
class Cat implements Animal {
public void speak() {
System.out.println("The cat meows: Meow!");
}
}
public class TestInterfacePolymorphism {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.talk();
myCat.talk();
}
}
In this example, the Animal
interface defines a talk()
method that must be implemented by any class that implements it. The Dog
and Cat
classes provide their own implementations of the talk()
method. In the main
method, references of type Animal
are used to invoke the mtalk()
method on Dog
and Cat
objects, demonstrating polymorphism.
Final Considerations
Polymorphism is a powerful tool in Java programming, allowing you to write code that is both general and specific. It is essential for creating robust and flexible applications. By understanding and correctly applying polymorphism, you can make the most of the object-oriented paradigm, creating systems that are easy to extend and maintain over time.