Runtime Polymorphism

    Runtime Polymorphism

    In Java, polymorphism is an object-oriented concept that allows you to perform a single task in different ways. Polymorphism is derived from poly and morphs which means many and forms which results in many forms.

    Java supports two types of polymorphism- runtime polymorphism and compile-time polymorphism. You can implement the polymorphism using method overloading and method overriding. Here we will discuss the runtime polymorphism in detail.

    What is runtime polymorphism?

    It is a method where you call an overridden method and get resolved at the runtime instead of the compile-time. You can access the overridden method using the instance variable of the parent class. It depends on the referred object to determine which method to call.

    What is upcasting in Java

    Upcasting is the method used during the method call. It refers to the process when we use the reference variable of the superclass to refer to the object of the subclass.

    Example-

    Class demo{}
    
    Class child_demo extends demo{}
    
    
    Now we will create the object of the child_demo class of class demo type-
    
    Demo d=new child_demo(); //this is upcasting

    Runtime polymorphism example-

    In the below example, the child_demo class is inheriting the demo class. Also, the child_demo class is overriding the demo class’s method - display. We are creating the object of the child_demo class with the demo class’s reference variable. The reference points to the subclass object and the subclass are overriding the parent’s class method then the method of the subclass will be invoked at the runtime.

    class Demo{  
    
      void display(){System.out.println("hello");}  
    
    }  
    
    class Child_demo extends Demo{  
    
      void diaplsy(){System.out.println("HELLO");}  
    
      
    
      public static void main(String args[]){  
    
        Demo b = new Child_demo();//upcasting  
    
        b.run();  
    
      }  
    
    }  

    Output-

    HELLO

    As this method invocation is done by JVM not compiler, thus is called runtime polymorphism.

    Implementing runtime polymorphism with data member-

    You can only override the method but not the data members. So you cannot achieve runtime polymorphism using the data members.

    Example-

    class Demo{  
    
    int var=100;}  
    
    
    
    class Child_demo extends Demo{  
    
     int var=30;
    
      
    
      public static void main(String args[]){  
    
        Demo b = new Child_demo();//upcasting  
    
        System.out.println(b.var);  
    
      }  
    
    }  

    Output-

    100

    Implementing runtime polymorphism with multiple inheritances-

    Example-

    class Demo{  
    
    int var=100;}  
    
    
    
    class Child extends Demo{
    
    int var=150;}
    
    
    
    class Child_demo extends Child{  
    
     int var=30;
    
      
    
      public static void main(String args[]){  
    
        Demo b = new Child_demo();//upcasting  
    
        System.out.println(b.var);  
    
      }  
    
    }  

    Output-

    100