mark
mark

Reputation: 2941

Java - calling super class and using standard methods

I'm wondering what the actual reason is why you can do this on a JFrame:

super("My Frame");

And it's the same as:

setTitle("My Frame");

I understand they are doing the same thing, but what is actually going on with each? Why would you call the super class instead of simply using the inherited methods?

Upvotes: 1

Views: 3604

Answers (2)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78639

The JFrame has a constructor that says

 public JFrame(String title) throws HeadlessException {
        super(title);
        frameInit();
    }

If you follow it, you eventually reach the java.awt.Frame class where in its constructor it says:

public Frame(String title) throws HeadlessException {
        init(title, null);
    }

Whose init method does:

private void init(String title, GraphicsConfiguration gc) {
    this.title = title;
    SunToolkit.checkAndSetPolicy(this, false);
}

Bottom line, you use super, simply because you are invoking that specific constructor. If you use any of the other JFrame constructors that won't receive the given string, then you can set the title manually as you suggest.

Upvotes: 2

Mayur
Mayur

Reputation: 676

when you say super("My Frame") you are calling constructor of super class.

and when you say setTitle("My Frame") its just setting title.

You can learn more about it at http://docs.oracle.com/javase/tutorial/java/IandI/super.html

Upvotes: 0

Related Questions