CAN YOU ACHIEVE RUNTIME POLYMORPHISM BY DATA MEMBERS IN JAVA?

Samuel Fernandes

Total Post:38

Points:190
Posted by  Samuel Fernandes
Java 
 4751  View(s)
Ratings:
Rate this:
  1. Mayank Tripathi

    Post:807

    Points:4035
    Can you achieve Runtime Polymorphism by data members in java?

    Method is overridden not the data members, so runtime polymorphism can't be achieved by data members.

    Example:
    Here in both the classes have a data member speedlimit, we are accessing the data member by the reference variable of Parent class which refers to the subclass object. Since we are accessing the data member which is not overridden, hence it will access the data member of Parent class always.
    class Bike{  
     int speedlimit=90;  
    }  
    class Honda3 extends Bike{  
     int speedlimit=150;  
      
     public static void main(String args[]){  
      Bike obj=new Honda3();  
      System.out.println(obj.speedlimit);//90  
    }

    Output: 90

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!