• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static overridden method

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What overridden method?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic