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?
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 needsuper.to distinguish it from your own implementation).The documentation for
JPanellists all these methods under the “Method Summary” header, specifically in the “Methods inherited from class java.awt.Container” section (there are multiple overloads ofaddwhich is why it is mentioned more than once there).