File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What's the difference between hiding and overriding? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's the difference between hiding and overriding?

Howard Ting
Greenhorn

Joined: Feb 17, 2004
Posts: 19
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

Joined: Jan 08, 2004
Posts: 74
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)


Ritesh<br /> <br />SCJP 1.4<br />IBM Test 340<br />IBM AIX V4.0 Certified Professional<br /> <br />Right actions for the future are the best apologies for wrong ones in the past.<br />- Tyron Edwards
Howard Ting
Greenhorn

Joined: Feb 17, 2004
Posts: 19
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
Consider Paul's rocket mass heater.
 
subject: What's the difference between hiding and overriding?