CoreJavaTutorials
Contribute Us | Ask Question |

Polymorphism in Java

Polymorphism is a concept by which we can perform a single action by different ways thats why it is also called as one name many forms. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. There are two types of polymorphism in java

1.Compile time polymorphism - Compile time polymorphism is a type of polymorphism when the calling to method is decided at compile time of a program.

2.Runtime polymorphism- Runtime polymorphism is a type of polymorphism when the calling to method is decided at run time of a program by using upcasting which we learn in runtime polymorphism .

We can perform polymorphism in java by method overloading and method overriding.So we first understand the difference between method overloading and method overriding

Method Overloading

If a class has two or more methods having same name but different in parameters, it is known as Method Overloading. It helps to increases the readability of the program.

For Instance, you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.So by applying method overloading we can have same method name add but with different arguments.

Example of Method Overloading

class Adding{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(10,15));
System.out.println(Adder.add(10,15,10));
}
}

Output


Output:
       25
       35
   

Method Overriding

If child class or a subclass has the same method and argument as declared in the parent class, it is known as method overriding in java. In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Rules for Java Method Overriding

1. Method must have same name as in the parent class
2. Method must have same parameter as in the parent class.
3. Must be IS-A relationship (inheritance).

Problem without Method Overriding

In below example we have to use the specific implementation of run method provide by parent class Vehicle if child class Bike have to give their on implementation of run method then it has to use method overriding.

class Vehicle{
void run(){System.out.println("Vehicle is audi ");}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}

Output


Output:  Vehicle is audi
       
   

Now Example with Method Overriding

In below example we have to use the specific implementation of run method provide by parent class Vehicle if child class Bike have to give their on implementation of run method then it has to use method overriding.

class Vehicle{
void run(){System.out.println("Vehicle is audi");}
}
class Honda extends Vehicle{
void run(){System.out.println("Vechile is Honda Bike");}
public static void main(String args[]){
Honda obj = new Honda();
obj.run();
}

Output


Output:  Vehicle is Honda Bike.