30

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();
}
Nazerke
  • 2,098
  • 7
  • 37
  • 57

4 Answers4

46

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)

cowls
  • 24,013
  • 8
  • 48
  • 78
  • 1
    In Java all classes are derived from Object, unless they are derived from another class. Therefore, super() refers to the constructor of the Object class. – Dohn Joe Mar 04 '13 at 17:04
  • 3
    Actually *ALL* Java classes are derived from Object. If they extend another class it just means they have a larger hierarchy. – cowls Mar 04 '13 at 17:06
  • 1
    hmmm, why would anyone refer to Object()'s constructor? if I delete this line with super() it won't have any effect will it? – Nazerke Mar 04 '13 at 17:09
  • 5
    Its a redundant line, as it is implied anyway. If you remove that line it will still call the super classes constructor. This ensures all constructors in the hierarchy are called – cowls Mar 04 '13 at 17:10
  • 2
    See the note at the bottom of this page: http://docs.oracle.com/javase/tutorial/java/IandI/super.html – cowls Mar 04 '13 at 17:11
  • I also ran across super() in constructor(s) of class(es) inheriting directly from Object - does it make any sense to keep it? Or could I freely remove it for the sake of readability? – rudolf_franek Jan 08 '14 at 17:52
  • Should be safe to remove. Note: it is called implicitly anyway if you do remove it. – cowls Jan 09 '14 at 09:06
  • 1
    If it does nothing then why eclipse auto generates `super()` while generating constructor even I am not using any inheritance. I think it does not make any sense using `super()` in constructor. – Yubaraj Feb 02 '16 at 11:31
19

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

jfmg
  • 2,626
  • 1
  • 24
  • 32
4

super calls the constructor of the extended class. All classes in Java derive from Object. Additionally, if the author of a class doesn't create a constructor for the class, a default constructor is created that does nothing.

In your case, super is calling the default constructor of Object.

If you'd like to learn more about Object, you can read the source code of Object.java here.

Zach Latta
  • 3,251
  • 5
  • 26
  • 40
-1

Now let me tell you the real reason why we use super() here that is if we want to call directly super-class constructor . when we call constructor then many constructor may be invoked in hierarchy,so to eliminate that we can use super(). During test-cases you might deal with "Do not provide any additional constructor other than what is asked in the problem statement" so, The solution is use Super() in your constructor.

disha
  • 1
  • 2