Polymorphism in Java

polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.
If you overload static method in java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

Runtime Polymorphism in Java

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
first understand the upcasting before Runtime Polymorphism.

Upcasting

  • method must have same name as in the parent class.
  • method must have same parameter as in the parent class.
  • must be IS-A relationship (inheritance).
  • Example of Java Runtime Polymorphism

    In this example, we are creating two classes Bike and Splendar. Splendar class extends Bike class and overrides its run() method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at runtime.
    Since method invocation is determined by the JVM not compiler, it is known as runtime polymorphism.

    Example

    class Car {  
    void run() {
    System.out.println("car is running");
      } }  
    class Alto extends Car {  
    void run() {
    System.out.println(" ca is running with 140km");}  
    public static void main(String args[]) {  
    Car c = new Alto();  //upcasting  
    c.run();  
      } }         
    

    Output:

    Car is running with 140km

    Example of method overriding

    we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes, so there is method overriding.

    Example:

    File: TestOverloading.java
    class Car {  
    void run() {
    System.out.println("Car is running");
      } }  
    class Alto1  extends Vehicle {  
    void run() {
    System.out.println("Alto  is running with speed");
      }  
    public static void main(String args[]){  
    Alto1  obj = new Alto1 ();  
    obj.run();  
      }  
    

    Output:

    Alto is running with speed

    Real example of Method Overriding

    Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, HDFC, PNB and IDBI banks could provide 4%, 6% and 8% rate of interest.

    Example:

    class Bank {  
    int getRateOfInterest(){
    return 0;
      } }  
    class HDFC extends Bank {  
    int getRateOfInterest() {
    return 4;
      } }  
    class PNB extends Bank{  
    int getRateOfInterest() {
    return 6;
      } }  
    class IDBI extends Bank {  
    int getRateOfInterest() {
    return 8;
      } }  
    class BankTest {  
    public static void main(String args[]) {  
    HDFC h = new HDFC();  
    PNB p = new PNB();  
    IDBI  i = new IDBI ();  
    System.out.println("HDFC Rate of Interest: "+s.getRateOfInterest());  
    System.out.println("PNB Rate of Interest: "+i.getRateOfInterest());  
    System.out.println("IDBI Rate of Interest: "+a.getRateOfInterest());  
     } }     
    

    Output:

    HDFC Rate of Interest: 4
    PNB Rate of Interest:  6
    IDBI Rate of Interest: 8
    

    Can we override static method?

    No, static method cannot be overridden. It can be proved by runtime polymorphism.

    Why we cannot override static method?

    because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.

    Can we override java main method?

    No, because main is a static method.

    Difference between method overloading and method overriding


    No.Method Overloading Method Overriding
    1.

    Method overloading is used to increase the readability of the program.

    Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
    2.
    Method overloading is performed within class.
    Method overriding occurs in two classes that have IS-A (inheritance) relationship.
    3.
    In case of method overloading, parameter must be different.
    In case of method overriding, parameter must be same.
    4.Method overloading is the example of compile time polymorphism.Method overriding is the example of run time polymorphism.
    5.In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.Return type must be same or covariant in method overriding.