Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the difference between hiding and overriding?

 
Howard Ting
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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!
 
Ritesh Agrawal
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
  • 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~
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic