gratsby
gratsby

Reputation: 3137

Hidden Methods in Java?

I am currently learning Java on my own, and this question has been puzzling me for a while, even though I'm sure the answer is a simple one. I was wondering whether there are hidden methods obtained through extending certain classes, such that you can call the method in the subclass without referencing super.method(... ? For example, take a look at this ButtonPanel class:

class ButtonPanels extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ButtonPanels(){
        JButton yellow = new JButton("Yellow");
        JButton blue = new JButton("Blue");
        JButton red = new JButton("Red");

        add(yellow);
        add(blue);
        add(red);
    }
}

The "add" methods here... where do they come from? It seems that writing super.add works perfectly fine, which confuses me. Don't you need to write super in front of all methods you call from a superclass?

Upvotes: 2

Views: 1111

Answers (4)

Scott
Scott

Reputation: 12366

This is an effect of polymorphism. The ButtonPanels class is both a ButtonsPanel and JPanel. As such it has access to most of the methods in JPanel as if they are in ButtonsPanel itself. The super becomes important if you overload a method that exists in the super class.

In your case, super.add(Object j) and add(Object j) are exactly the same when you call them because add is not overloaded.

To add, polymorphism is the concept that an object can take multiple forms. It is itself and all super classes at the same time and can referenced as such. In the case of this button panel all the below are different ways to construct the object.

JButton j = new JButton();
Container c= new JButton();
Object o = new JButton();

(Its late and I hope I don't have these backwards, someone please let me know if I do)

Upvotes: 0

Snake Eye
Snake Eye

Reputation: 535

JVM/Compiler is smart enough to check the availability of method. It check if the method is available in current class. If it is , it uses the one available. If not , it checks the super class. If available there it uses it. If not it goes one more level up till it finds the implementation. The compiler gives error about method not found if it do not find it any class.

Upvotes: 0

kosa
kosa

Reputation: 66637

You don't need to write super. If the method definition is not available in this class, jvm checks for the method availability in super class.

super is required, if you have overrriden method and would like to call super class implementation instead of sub class implementation (overriden implementation)

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 993085

These are not "hidden" methods. The super. is optional when calling a method from the superclass (as long as you haven't overridden it in your own class, then you need super. to distinguish it from your own implementation).

The documentation for JPanel lists all these methods under the "Method Summary" header, specifically in the "Methods inherited from class java.awt.Container" section (there are multiple overloads of add which is why it is mentioned more than once there).

Upvotes: 5

Related Questions