0

Possible Duplicate:
Overriding vs Hiding Java - Confused

Can someone please explain method hididng in Java with an example and why we use method hiding?

I read the tutorial of Oracle but couldn't understand.

Community
  • 1
  • 1
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
  • 7
    Can you give us a concrete example of what you don't understand in the tutorial, along with your interpretation of what is going on? – Hans Z Jul 23 '12 at 15:04
  • What does method hiding do exactly and why we use it? – Salih Erikci Jul 23 '12 at 15:05
  • **Method hiding** has to be used when you want to have a flexibility to call super class method or child class method based on your need. **Overriding** is used when your super class method need not be executed. i.e, if you think that, your super class method is no more useful to you and your child class method fulfills all the requirements, then go for method overriding. – Lion Jul 23 '12 at 15:06
  • The tutorial in question is [this one](http://docs.oracle.com/javase/tutorial/java/IandI/override.html), which is a bit confusing. – Roddy of the Frozen Peas Jul 23 '12 at 15:07
  • This sounds like some fairly simply to explain object-oriented principle, but it's difficult to tell exactly what you mean by "method hiding". Private methods? Something else? You referenced a tutorial, is there an example you can show us? – David Jul 23 '12 at 15:08
  • @David: There's an example in the tutorial. Basically, method hiding means selecting methods based on the type of the variable rather than that of the object. If you have a class `A` and a class `B` which extends it, method hiding means that `A a = new B(); a.method();` will call `A.method` regardless of whether there's a `B.method`. In order to call `B.method`, you'd have to call it through a variable of type `B` (or, in Java because of how it works there, call it statically). Java does it using static methods, as it doesn't have non-virtual instance methods. – cHao Jul 23 '12 at 15:30
  • @cHao: Makes sense. As I thought, it's a fairly straightforward OO concept, I just wasn't aware of the term. Thanks! – David Jul 23 '12 at 15:37

1 Answers1

0

When you have static method in the super class and if you create a method with same signature in the sub class, the super class method will get called if you are using the super class reference and sub class method gets called if you are using the sub class reference.

Varun
  • 1,014
  • 8
  • 23
  • This is wrong - it does not apply to statics, you cannot hide a static method, for you cannot inherit it either. – Romain Jul 23 '12 at 15:11
  • 1
    @Romain: You might want to check the tutorial that was linked. It itself says that hiding applies *only* to statics (they say "class methods", as opposed to non-static "instance methods"). May be Java-specific, but that's how they use the word. And it's consistent with how C#'s `new` and VB's `Shadows` work, although they (unlike Java) make instance methods non-virtual by default. – cHao Jul 23 '12 at 15:15