wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Redefining a static method in subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Redefining a static method in subclass" Watch "Redefining a static method in subclass" New topic
Author

Redefining a static method in subclass

Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Friends,

Have a look at the following code:



Compiling and running this code prints: From SuperTest

Q. Can someone please explain why it prints "From SuperTest" and not "From SubTest" since the inherited instance method printa() is invoked on subclass reference type which I think should be considered for invoking the "redefined / redeclared" static method defined in the subclass??

---------
Ravinder
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Think of it like this... the printa() method is being run from SuperTest and thereby calls the super method. Override this method in the sub class and the result will be different.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Thanks John...
Terry Huang
Greenhorn

Joined: May 16, 2006
Posts: 25
hey John could you elaborate on that? I didn't quite understand you..

thx
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Once you over ride the method printa() , the method a() in the sub class will get called from the subclass version of printa() . Else the method in the super class is called. So when the method is over ridden it prints "From SubTest".
Terry Huang
Greenhorn

Joined: May 16, 2006
Posts: 25
Sorry, I meant to ask if you could elaborate on why it prints "From SuperTest" not "From SubTest". Can anyone tell me why?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might be interested in this: Overriding vs. Hiding.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
In SuperTest class, compiler will hader code "void printa() { a(); }" to "void printa() { SuperTest.a(); }"
See Ernest's post in this thread.
[ May 21, 2006: Message edited by: wise owen ]
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Thanks Wise...that thread has the real explanation.

So..the compiler sees the above code as:



and therefore invokes the static method from super class as decided and hardcoded by the compiler at compile-time into the class file.

----------
Ravinder
Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
static methods can't be overridden.


SCJP 5.0<br />SCWCD 1.4<br />SCBCD 1.3<br />SCEA part 1
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Redefining a static method in subclass
 
Similar Threads
Confused with static methods
Overloading or Overriding ?
Constructor
Inheritance
over ridden method and super constructor