rohit shekhar wrote:
in this code it prints class b, it means we can access subclass method from superclass ref... but in previous case it gives error... 1st prog suggests we cant access subclass methods from superclass but if we will override it then that rule vanish???
I think you are reading too much into it... you are using an "a" reference, which means it is an object that IS-A "a".
In the first example, the a class does not have a disp() method, and in the second case, the a class does.
Rohit, use full code snippets rather then the short cuts.
In your first example, if you watch carefully, there is no method like disp() in class A. You define that method in class B.
So you are not really overriding disp() but actually you are trying to invoke a method which is present in class B using reference
of class A, which is not possible, because compiler cannot see anything like method disp() in class A.