What is Polymorphism in Java

In this article we learn what is polymorphism in java, polymorphism in java examples, types of polymorphism in java and what is method overloading and overriding in java.

What is Polymorphism in Java?

Poly and Morphs. The word “poly” means many and “morphs” means forms. So, polymorphism means many forms. The virtue by which the same action can be performed by objects of different classes and each object responds in a different way depending on its class is called Polymorphism.

Polymorphism in Java Examples

1. Security Guard

A security guard outside an organization behaves differently with different people entering the organization. He acts in a different way when the Boss comes and, in another way when the employees come.

When the customers enter, the guard will respond differently. So here, the behavior of the guard is in various forms, which depends on the member who is coming.

2. Smart Phone

One can use a Smartphone for calling. The same device can be used to listen to music or to watch videos. So, with the help of one Smartphone, we can do several different kinds of tasks with the help of various applications.

3. Man

A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So, the same person possesses different behavior in different situations. This is called polymorphism.

Types of  Polymorphism in Java

1. Compile-time Polymorphism or Method Overloading

2. Runtime Polymorphism or Method Overriding

Method Overloading in Java | Compile-time Polymorphism in Java

When a class has two or more than two methods which are having the same name but different types of order or number of parameters, it is known as Method overloading. Java allows a function to have the same name if it can distinguish them by their number and type of arguments. It is also known as compile-time polymorphism.

Compile-Time Polymorphism in Java Example | Method Overloading in Java Example

package javaPractice;

public class CompiletimePolymorphism {

void testMethod() {

System.out.println(“Test Method”);
}

void testMethod(int a) {

System.out.println(“Test Method with Parameter”);
}

public static void main(String[] args) {

CompiletimePolymorphism obj = new CompiletimePolymorphism();
obj.testMethod();
obj.testMethod(10);
}

}

Output:

Test Method
Test Method with Parameter

We can overload a method using three different ways:

1. Number of arguments

add (double, double)

add (double, double, double)

2. Datatype of parameters

add (int, double) add (float, int)

3. Sequence of parameters

add (float, int) add (int, float)

Method Overriding in Java | Runtime Polymorphism in Java

Method Overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class. Overriding allows a child class to provide the specific implementation of a method that is already present in its parent class. It is also known as runtime polymorphism.

Method Overriding can only be achieved in different classes and only with the help of Inheritance.

Runtime Polymorphism in Java Example | Method Overriding in Java Example

 package javaPractice;

public class ParentClass {

void testMethod() {

System.out.println(“Parent Class Test Method”);
}

}

package javaPractice;

public class ChildClass extends ParentClass {

void testMethod() {

System.out.println(” Child Class Test Method”);
}

public static void main(String[] args) {

ChildClass obj = new ChildClass();
obj.testMethod();
}

}

Output:

Child Class Test Method

Difference between Method Overloading and Method Overriding in Java

Sr No

Method Overloading

Method Overriding

 

1

Must have at least two methods   by the same name in the class.

Must have at least one method by the same name in both parent and child classes.

 

 

2

Must have a different number of parameters.

Must have the same number of parameters

3

If the number of parameters is  the same, then it must have different types of parameters.

Must have the same parameter types.

 

Other Popular Articles

What is Encapsulation in Java OOPs Concept?

Leave a Comment