• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overriding static methods in subclass

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to take a look at this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic