Hi all I'm having a bit of trouble wrapping my head around exactly what hiding a class method does, can somebody try giving it to me in plain English? I'm going by the Sun Java Tutorial but I find the example a bit vague when it comes to the hiding. I have no problem with overriding methods that seems simple enough I just can't wrap my head around why you would want to hide a class method in a subclass instead of creating a new method specific to the subclass.

Recommended Answers

All 6 Replies

Method hiding is same as method overriding.
In some cases, it requires that the interface will be same but the implementation will be different for superclass and subclass or the superclass method is a generic method and subclass need to implement some specific implementation. In that cases, you need to override method to hide the method in superclass. Consider the following example -

class shape {
    public void area(int a, int b) {
        System.out.println("generic area");
    }
}
public class square extends shape {
    public void area(int a, int b) {// method overridden to implement square specific area
        System.out.println("square area");
    }
}

Thanks for the example, in this case -

square mysquare = new square();
shape myshape = new shape();
mysquare.area(1, 2);
myshape.area(1, 2);

output for line 3 should be "square area" and line 4 should be "generic area"?

Yes, you are right.

Thanks for the example, in this case -

square mysquare = new square();
shape myshape = new shape();
mysquare.area(1, 2);
myshape.area(1, 2);

output for line 3 should be "square area" and line 4 should be "generic area"?

Great thanks, I think the java tutorial overcomplicated it a lot and you've helped boil it down for me.

It's like overriding, it is not "the same as" overriding. Overriding makes it impossible to use the overridden method without instantiating anew instance of the class containing it (not a class that extends that class). Static methods can always be used. Which one you use depends on which Class reference you use to access it (and you should always use a Class reference, not an instatiated instance, to acess them). The tutorial does not "overcomplicate" it, the previous "advice", IMHO, over simplified it. The tutorial is only thorough, but also brief.

Hi all I'm having a bit of trouble wrapping my head around exactly what hiding a class method does, can somebody try giving it to me in plain English? I'm going by the Sun Java Tutorial but I find the example a bit vague when it comes to the hiding. I have no problem with overriding methods that seems simple enough I just can't wrap my head around why you would want to hide a class method in a subclass instead of creating a new method specific to the subclass.

If i understand what you are referring to correctly, then it all comes down to code reuse. Why create a new method when you already have one there. Plus if need be, you can add more functionality to the method. Like appending more data to the superclasses data already exsisting in that method.
Also, I believe Polymorphism has a good play on what you can do with overriding methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.