Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

super() function in JAVA

Tags:

java

super

Button class:

class SubmitButton extends JButton implements ActionListener {
    public SubmitButton(String title){
        super(title);
        ....

Where I declare it:

SubmitButton submit = new SubmitButton("Submit");
submit.setBounds(530+150, 200, 100, 25);

How does super(title) set the String title to the title of the button? How does it do the same as the method .setText() for regular JButtons?

In other words, how did calling super() do the same thing as .setText() how does super() know to change title? is .setText() the only method in JButton Class that takes in string as parameter?

like image 443
Devoted Avatar asked Dec 20 '08 03:12

Devoted


4 Answers

  • SubmitButton extends JButton
  • JButton has a constructor which might look like this (simplified):

    public JButton(String title) {
        setText(title);
    }
    
  • SubmitBUtton has a constructor:

    public SubmitButton(String title) {
        super(title);
    }
    

The SubmitButton constructor is calling the superclass (JButton) constructor, which is in turn calling setText. Now internally, JButton might work differently, but the effect is the same.

The overall point is that super(...) calls the superclass constructor.

like image 72
Draemon Avatar answered Sep 21 '22 08:09

Draemon


JButton is a central Java Swing class that supports multiple constructors. One of these constructors allows you to set the text for the button label.

Adding super(title) didn't actually make a call - the constructor of the parent would have been called anyway, but it helped select which constructor is invoked.

The setText() operation allows you to change text after button is created but (usually) before it is displayed.

like image 43
Uri Avatar answered Sep 22 '22 08:09

Uri


When the SubmitButton is initialized with the constructor SubmitButton(String title), you call the parent/super class constructor with the title. If you didn't specify the super(title), then the Java compiler would automagically insert the code to call the default constructor of the super class. Then the title would not be set when you create the SubmitButton.

Also, the superclass (JButton) might be using the .setText(string) within its constructor and that is why it performs the same function (need to look at the actual Java source).

http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html?page=2
http://leepoint.net/notes-java/oop/constructors/constructor.html

like image 33
kevindaub Avatar answered Sep 24 '22 08:09

kevindaub


In all cases in java, that "call" to super() invokes the parent class's ctor. Like all functions, when you call a ctor, Java pattern matches the name and parameter types. JButton declares a ctor with string argument, so when you call super(title) the effect is that you're invoking the constructor for the immediate superclass (JButton) that takes a single string argument.

like image 42
Charlie Martin Avatar answered Sep 22 '22 08:09

Charlie Martin