简体   繁体   中英

Why do we call super() in constructor

We have a constructor like this:

public statusArea()     
{           
   super();         
   add(pageIdentifier);     
}

Why do we call super in it?

From Oracle docs

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. Object does have such a constructor, so if Object is the only superclass, there is no problem.

super() invokes the parameterless constructor of the super class.

In your code it makes no difference, since the compiler will add super() implicitly if you don't. In general, you add an explicit call to the super class's constructor if you wish to call a specific constructor of the super class.

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

The syntax for calling a superclass constructor is

super(); // the superclass no-arg constructor is called

or:

super(params);//the superclass constructor with a matching params 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. Object does have such a constructor, so if Object is the only superclass, there is no problem.

The purpose of the super keyword in Java is to allow you to access the instance fields, instance methods or the constructor of a super class from which a class extends.

Since every class in Java extends from Object implicitly, a call to super() in a constructor for a class that does not extend from any class means a call to the no-arg constructor in the Object class.

Even if you don't put the super() call explicitly in a constructor, the compiler will add one.

Here you use super() to give the parent class (ie. the class which this one extends) a chance to initialize any fields or variables, so that your sublcass can work correctly. For example, the superclass may have some fields or variables which you do not use in your subclass, but which are needed for the class to work correctly.

In this case, super() invokes the parameterless constructor of the superclass, ie. the one which your class extends.

If you call super(...) with parameters, then the superclass constructor with the matching parameters is called.

As others have said, if you don't call super() yourself, the compiler will call it.

You only need to specify the constructor to call if:

You want to call a superclass constructor which has parameters

You want to chain to another constructor in the same class instead of the superclass constructor

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