This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can static method overridden? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can static method overridden?" Watch "can static method overridden?" New topic
Author

can static method overridden?

derek_ho
Greenhorn

Joined: Jan 24, 2002
Posts: 4
Is this true:
"static methods can be overridden by static methods only".( from java_cert exam)
The answer is false. can someone tell me why?
can static method be overriden? If yes, what are ways of overriding a static method?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by derek_ho:
Is this true:
"static methods can be overridden by static methods only".( from java_cert exam)
The answer is false. can someone tell me why?
can static method be overriden? If yes, what are ways of overriding a static method?

No, it is not true. Static methods cannot be overridden, rather, they can be hidden.
This program shows the difference between method hiding and overridding.

As you can see, line 1 works as expected. You are making a method call on an SuperClass object reference variable, but because the actual instance is of type SubClass, the overridden method gets called.
In line 2, when we attempt to do the same thing with a static method, we see that the same rules do not apply. The static method that gets invoked is the one that is a member of the reference variable's class, NOT the one that is a member of the actual object instance!
This shows that there is a very real, tangible, important distinction between static and instance methods, and between hiding and overridding. It also shows why it is Bad Form to refer to a static member by an reference variable; you should always use the class name to access a static member. In the above line 2, if you had written
SubClass.staticPrintClassName();
SuperClass.staticPrintClassName();
it would be clear which method would be invoked.

Rob


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
See also the following thread for a good discussion about the subject:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014224
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
Rob,Based on the code,how to invoke the method printClassName() of superClass?


help you means help me
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Seany,
there are only two ways.
One, instantiate an object of type SuperClass and call that method.
SuperClass sc = new SuperClass();
sc.printClassName();
Two, create a subclass of SuperClass. Subclasses can call super.printClassName() and access the overridden method.

Rob
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
Two, create a subclass of SuperClass. Subclasses can call super.printClassName() and access the overridden method

Rob, thank you.
I know the first way you told.
But as for the second,It's only be called in subclass? If out the subClass, can it?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
No Seany. If you have an object instance of type SubClass, and you call printClassName()on that object, there is no way for you to invoke the SuperClass version. The only method available to you is the overridden version.
Rob
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I see,thank you very much,Rob!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can static method overridden?