Polymorphism In Java
Polymorphism literally means “having many forms” and is used as a feature in
object oriented programming that provides one interface for a general class of
actions. The specific action is implemented to address the exact nature of the
situation.
Example 1:
class Animal {
/*Animal is a parent class*/
void makeSound()
{
System.out.println(“Now Speak!”);
}
}
class Cat extends Animal {
/*Cat is a child class*/
void makeSound()
{ System.out.println(“Meow”);
}
}
class PolymorphismExample
{
public static void main(String args[]
)
{
Animal a = new Animal();
/*creating object*/
Cat d = new Cat();
/*creating object*/
a.makeSound();
d.makeSound();
}
}
Output:
Now Speak!
Meow
Types of Polymorphism
OVERLOADING OF METHOD:
Method overloading is one of the ways through which polymorphism is
supported by Java. Method overloading is a concept of declaring multiple methods
with the same name and different parameters of the same or different data types in
the same class. Method overloading is compile-time or static polymorphism.
There are three ways to overload a method:
1. There are different number of parameters in the argument list of the methods.
Example: sum(int, int)
sum(int, int, int)
2. There are different data types of the parameters in the argument list.
Example: sum(float, float)
sum(float, int)
3. There is a different sequence of data types of the parameters.
Example: sum(float, int)
sum(int, float)
Example:
public class MethodOverloadingExample {
// Overloaded methods
public void display() {
System.out.println("Display method with no arguments");
}
public void display(String message) {
System.out.println("Display method with one argument: " + message);
}
public void display(int number) {
System.out.println("Display method with an integer argument: " + number);
}
public static void main(String[] args) {
MethodOverloadingExample obj = new MethodOverloadingExample();
obj.display(); // Calls method with no arguments
obj.display("Hello, Overloading!"); // Calls method with String argument
obj.display(42); // Calls method with integer argument
}
}
OVERRIDING OF METHOD
In a class hierarchy, when a method in the subclass has the same name
and parameters as in its superclass, that method is said to override the
method in superclass.
When an overridden method is called from its subclass, then always
the version of subclass method will be referred to, and the version of the
method of superclass will remain hidden.
This mechanism provides an opportunity for the subclass to have its
own specific implementations of the overridden methods.
Overriding the method in Java is one of the ways to achieve runtime
polymorphism
Example:
// Parent class
class Parent {
public void show() {
System.out.println("Show method in Parent");
}
}
// Child class
class Child extends Parent {
@Override
public void show() {
System.out.println("Show method in Child (Overridden)");
}
}
public class MethodOverridingExample {
public static void main(String[] args) {
Parent parent = new Parent();
parent.show(); // Calls Parent's show() method
Child child = new Child();
child.show(); // Calls Child's overridden show() method
Parent parentRef = new Child();
parentRef.show(); // Calls Child's overridden show() method (runtime
polymorphism)
}
}