Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

super() in constructor

I'm reading through some code. In the constructor it has super() but the class implements interface which of course doesn't have a constructor. So which super() it is referring to?

public class BoundingBox implements IBoundingVolume {  public BoundingBox() {         super();         mTransformedMin = new Number3D();         mTransformedMax = new Number3D();         mTmpMin = new Number3D();         mTmpMax = new Number3D();         mPoints = new Number3D[8];         mTmp = new Number3D[8];         mMin = new Number3D();         mMax = new Number3D();         for(int i=0; i<8; ++i) {             mPoints[i] = new Number3D();             mTmp[i] = new Number3D();         } }   public interface IBoundingVolume {     public void calculateBounds(Geometry3D geometry);     public void drawBoundingVolume(Camera camera, float[] projMatrix, float[] vMatrix, float[] mMatrix);     public void transform(float[] matrix);     public boolean intersectsWith(IBoundingVolume boundingVolume);     public BaseObject3D getVisual(); } 
like image 276
Nazerke Avatar asked Mar 04 '13 17:03

Nazerke


People also ask

Where super () can be used within a constructor?

super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.

What is super () in constructor Java?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

Why do we call super in constructor?

We use super keyword to call the members of the Superclass. As a subclass inherits all the members (fields, methods, nested classes) from its parent and since Constructors are NOT members (They don't belong to objects.

Is Super necessary in constructor?

If the SuperClass has a default Constructor there is no need to call it using"super()" explicitly, it will be invoked implicitly.


2 Answers

super() refers to the extended class (not an implemented interface). Which in this case is Object

So it will call the constructor in Object (Which does nothing)

like image 190
cowls Avatar answered Oct 03 '22 22:10

cowls


Super is referencing to the extended class. By default it is the Object class. The constructor in Object does nothing. In other words you can delete this line as it is not necessary.

Please also note what Oracle is saying about this topic:

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.

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

like image 33
jfmg Avatar answered Oct 03 '22 22:10

jfmg