我什么时候使用 super() ?

我目前正在学习 Java 课程中的类继承,但是我不知道什么时候使用 super()调用?

编辑:
我找到了使用 super.variable的代码示例:

class A
{
int k = 10;
}


class Test extends A
{
public void m() {
System.out.println(super.k);
}
}

所以我理解在这里,您必须使用 super来访问超类中的 k变量。然而,在任何其他情况下,super();做什么?自己?

182585 次浏览

You could use it to call a superclass's method (such as when you are overriding such a method, super.foo() etc) -- this would allow you to keep that functionality and add on to it with whatever else you have in the overriden method.

The first line of your subclass' constructor must be a call to super() to ensure that the constructor of the superclass is called.

When you want the super class constructor to be called - to initialize the fields within it. Take a look at this article for an understanding of when to use it:

http://download.oracle.com/javase/tutorial/java/IandI/super.html

You would use it as the first line of a subclass constructor to call the constructor of its parent class.

For example:

public class TheSuper{
public TheSuper(){
eatCake();
}
}


public class TheSub extends TheSuper{
public TheSub(){
super();
eatMoreCake();
}
}

Constructing an instance of TheSub would call both eatCake() and eatMoreCake()

You call super() to specifically run a constructor of your superclass. Given that a class can have multiple constructors, you can either call a specific constructor using super() or super(param,param) oder you can let Java handle that and call the standard constructor. Remember that classes that follow a class hierarchy follow the "is-a" relationship.

super is used to call the constructor, methods and properties of parent class.

Calling exactly super() is always redundant. It's explicitly doing what would be implicitly done otherwise. That's because if you omit a call to the super constructor, the no-argument super constructor will be invoked automatically anyway. Not to say that it's bad style; some people like being explicit.

However, where it becomes useful is when the super constructor takes arguments that you want to pass in from the subclass.

public class Animal {
private final String noise;
protected Animal(String noise) {
this.noise = noise;
}


public void makeNoise() {
System.out.println(noise);
}
}


public class Pig extends Animal {
public Pig() {
super("Oink");
}
}

You may also use the super keyword in the sub class when you want to invoke a method from the parent class when you have overridden it in the subclass.

Example:

public class CellPhone {
public void print() {
System.out.println("I'm a cellphone");
}
}


public class TouchPhone extends CellPhone {
@Override
public void print() {
super.print();
System.out.println("I'm a touch screen cellphone");
}
public static void main (strings[] args) {
TouchPhone p = new TouchPhone();
p.print();
}
}

Here, the line super.print() invokes the print() method of the superclass CellPhone. The output will be:

I'm a cellphone
I'm a touch screen cellphone

From oracle documentation page:

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.

You can also use super to refer to a hidden field (although hiding fields is discouraged).

Use of super in constructor of subclasses:

Invocation of a superclass constructor must be the first line in the subclass constructor.

The syntax for calling a superclass constructor is

super();

or:

super(parameter list);

With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.

Related post:

Polymorphism vs Overriding vs Overloading

I just tried it, commenting super(); does the same thing without commenting it as @Mark Peters said

package javaapplication6;


/**
*
* @author sborusu
*/
public class Super_Test {
Super_Test(){
System.out.println("This is super class, no object is created");
}
}
class Super_sub extends Super_Test{
Super_sub(){
super();
System.out.println("This is sub class, object is created");
}
public static void main(String args[]){
new Super_sub();
}
}