简体   繁体   中英

Extending JFrame - how does “super” keyword work?

I've made a class that extends JFrame , and in my classes constructor, to set the title I can either use:

super("title");
setTitle("title");

or just create a JFrame object ie:

JFrame f = new JFrame("title);

How does the compiler differentiate from all of these to conclude the same thing? I know super is used to call overridden methods in the superclass, but how does it specifically know to call the setTitle method?

My idea is that super calls the constructor of JFrame class, and there is only one constructor which takes a string parameter, so it's basically doing: JFrame f = new JFrame("title); , without explicitly creating the object.

Is this true? If not, can someone explain what super is exactly doing, and why setTitle does not need an object reference?

Cheers

  1. new JFrame( "title" ) calls JFrame 's constructor. It may call setTitle() (but it probably doesn't), or it may just set the internal variable used to hold the title.

  2. super.setTitle() is just a method call. If you have overridden setTitle() , it'll ignore your definition and call the same method on the super class. It's normally used for "chaining" to a super class's methods.

  3. super( "title" ) can only be called as the first line of a constructor. It is not a method call. It calls a specific constructor in the super class, constructs that object, then continues with your constructor code. This is the way all constructors work. If you don't specify a constructor, the no-argument constructor ( super() ) is called automatically.

If you look at the Frame Object, calling super (JFrame) means that you are passing the parameter into JFrame so JFrame jf = new JFrame("Title"); is the same as super("Title"); (assuming super type is JFrame)

When you call super("title") in your super class, you tell Java "to instantiate my object, first call the constructor on the super class that takes a String argument". If you don't have an explicit super call, then the no-arg constructor is called (as is always the case with the Object() constructor). It turns out that that JFrame constructor calls setTitle with its argument.

When you call setTitle("title") , this is implied, so you call the method on your object. If you haven't overridden it, then the super class implementation is called.

how does it specifically know to call the setTitle method?

Calling super() by itself will call a superclass constructor.

When you call setTitle() java looks to see if that method exists in the current class. If it doesn't, it will look at the parent class.

If you've overridden setTitle() , calling setTitle() from within the child class will call that class' method. If you call super.setTitle() it will call the setTitle() method in the super class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM