• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

something related to overriding

 
rohit shekhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i think I am asking silly silly question but what to do when earlier i studied Completed Ref by Schild it was ok for me but this book(SCJP by sierra), each page is increasing my confusion day by day.
 
Henry Wong
author
Marshal
Pie
Posts: 20886
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit shekhar wrote:


Rohit,
It will be easier to read the code if public static void main is used instead of psvm.
See JavaRanch rules about Post Real Code
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rohit,

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.

There is no overriding in example 1.

Hope this helps,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic