Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static overridden method

 
kedar parundekar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Fig
{
--------
-------
static void show()
{
System.out.println("In Fig show()");
}
}
class Rect extends Fig
{
-------
-------
static void show()
{
System.out.println("In Rect show()");
}

public static void main(String args[])
{
Fig f;
f =new Rect(5);
f.show();
}

}// end of class

Question : why f.show() statement invoke show method from Fig class ? why not from invoke overidden method from Rect class?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What overridden method?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods are not polymorphic. That means that the compiler decides what method to call at compile time based on the static type of the reference. Since you're calling the method via a reference of type "Fig", the compiler hard-codes "Fig.show()" into the class file. No dynamic lookup is done at runtime.

When you redefine a static method in a subclass, you're merely "hiding" the base class's method -- you're not overriding it, because the term "overriding" implies polymorphic behavior.
 
Venkat Paspula
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kedar parundekar,
First of all,Did your code compile?.The follwoing will not compile.Because there is no constructor defined.
Fig f = new Rect(5);

And also you cann't override static methods.
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are Static methods inherited ? , i think they are but why. Static methods / variables are class property and not object property,then why are inherited .


tjanx in advance .
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neha Mohit:
Static methods / variables are class property and not object property,then why are inherited .


What does the first have to do with the second in your opinion?
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


What does the first have to do with the second in your opinion?



I think , class property should be specific to the class.


regards
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neha Mohit:
I think , class property should be specific to the class.


Well, I guess from a purely philosophical point of view, you might be right.

But the more important question to answer for me is: would it make the live of the programmer easier or harder?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic