Home > DeveloperSection > Interviews > Can you achieve Runtime Polymorphism by data members in java?

Posted on    April-02-2015 12:24 AM

 Java Java 
 1 Answer(s)
  4431  View(s)
Rate this:

Mayank Tripathi
Mayank Tripathi

Total Post:807

Posted on    April-02-2015 1:25 AM

Method is overridden not the data members, so runtime polymorphism can't be achieved by data members.
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();  

Output: 90

Don't want to miss updates? Please click the below button!

Follow MindStick