Polymorphism In Java with example

Polymorphism In Java with example

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 Addition {
  int add(int a, int b) {
    return a + b;
  }
  int add(int a, int b, int c) {
    return a + b + c;
  }
}
class Main {
  public static void main(String[] args) {
    Addition addition = new Addition();
    System.out.println(addition.add(23, 45));
    System.out.println(addition.add(30, 15, 33));
  }
}

Output

68
78

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 Nexa");
  }
}
public class Car extends Vehicle {
  public static void main(String args[]) {
    Car obj = new Car();
    obj.run();
  }
}

Output

Vehicle is Nexa

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");
  }
}
public class TVS extends Vehicle {
  void run() {
    System.out.println("Vehicle is TVS Bike");
  }
  public static void main(String args[]) {
    TVS obj = new TVS();
    obj.run();
  }
}

Output

Vehicle is TVS Bike