wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes something related to overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "something related to overriding" Watch "something related to overriding" New topic
Author

something related to overriding

rohit shekhar
Ranch Hand

Joined: Mar 05, 2010
Posts: 32


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
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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

SCJP 6
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: something related to overriding
 
Similar Threads
Methods
flow of execution
Overriding
static method overriding
Package Problem...