This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding static methods in subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding static methods in subclass" Watch "Overriding static methods in subclass" New topic
Author

Overriding static methods in subclass

Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
This is the information I got from the sun tutorial regards to overriding static methods: http://java.sun.com/docs/books/tutorial/java/javaOO/override.html
Quote from the same site:
"Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass."

From the above quote, the sentences contradict each other.
When they say a subclass can hide a static method in the superclass with the same signature, isn't it the same as overriding, duh? (unless I am missing something here).

Thanks!
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
Nina,


Overriding means you are providing a more specialized version in your subclass. The overridden method is still accessible if desired.
You can still access the overridden method by using the keyword super in your subclass.

The declaration of a static method in a subclass will "hide" or make your method in the superclass not accessible any longer through the object reference of the subclass.
You cannot use the keyword super in a static context to access the hidden method.

Example:


Overriding


The output is: B


Hiding

The output is: A


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
John Ip
Ranch Hand

Joined: Mar 04, 2004
Posts: 45
Hi Nina,

In my opinion, there is no contradiction in the tutorial. For me, hide is the word for static methods/variables. And overrides is the word for others. They are doing the same thing when you are coding.
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi,
Overriding is used to avail polymorphism. If you override a method in a base class then you can access the sub class version through a base class reference variable which is referencing a subclass object.

If you are hiding (or in other words defining a method with same name as in the super class), u cant access the subclass version through the super class reference.(Or no polymorphism)
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
I understand now the difference between the terms overriding and hiding. Thank you Thomas for the cool example, Thank you John and Vipin for the explanations.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
You might also want to take a look at this.


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding static methods in subclass
 
Similar Threads
Hiding or overriding static methods
inheritence in case of private and static
static method overriding static method
what is the blaze result?
overriding static methods in subclass