Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Redefining a static method in subclass

 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John...
 
Terry Huang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey John could you elaborate on that? I didn't quite understand you..

thx
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in this: Overriding vs. Hiding.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods can't be overridden.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic