• 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

What's the difference between hiding and overriding?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm a little confused with hiding and overriding. What're the definitions and usages of them? I mean, in what situation am I to hide a method and in what situation am I to override one?
In both situation I can always use the method using super or parent's name whether it's hidden or overridden. Does it achieve something in distinguishing between these two nouns?
Please help me out!
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only difference I can think of here is that when the method declared in parent class is a static method, then it is hidden by the derived class static method with same signature. Non-static methods in base class can be overridden by derived class method with same signature.
For example,

Here is a helpful link to get more detailed information about this topic.
SCJP Questions & Answers by Roseanne Zhang (2)
Ritesh
(Preparing for SCJP)
 
Howard Ting
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply and that article!
Now I know the differece in ways to use of hiding and overriding, which prove what my teacher said, "static along with class, instance along with instance." However, I think that's just the rules of overriding, inheritance, and OO.
I mean, the following statement (from that example):
Base base = new Sub();
implies base is referenced to an instance of Sub. When we call an overridden method in the runtime, it will really execute the statements of that method in Sub. Isn't that just the rule of Java in OO? Namely, calling a static method with the class name (or reference name) is just the way of OO. I don't see any necessity of emphasizing hiding or overriding. I wonder if we can only show the difference between hiding and overriding in this tricky way.
This is just my presumption, you guys can put it aside~
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One word: polymorphism. Static methods do not participate in polymorphism so they are hidden, not overridden.
What this means is that static methods are assigned at compile time while non-static/non-final methods are assigned at run time.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic