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
Liked By
Write Answer
Can you achieve Runtime Polymorphism by data members in java?
Join MindStick Community
You have need login or register for voting of answers or question.
Anonymous User
02-Apr-2015