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

Overriding static methods in subclass

 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Ip
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to take a look at this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic