aspose file tools*
The moose likes Beginning Java and the fly likes Static overridden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static overridden method" Watch "Static overridden method" New topic
Author

Static overridden method

kedar parundekar
Ranch Hand

Joined: May 10, 2006
Posts: 40
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

Joined: Sep 16, 2005
Posts: 1780
What overridden method?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Venkataramana Pasula
Greenhorn

Joined: Jan 31, 2006
Posts: 8
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.


Thanks<br />Venkataramana Pasula
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
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

Joined: Jul 11, 2001
Posts: 14112
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static overridden method