2
package Geometry;

public abstract class GeometryFigures{

protected double surfaces;
protected double perimeter;

public GeometryFigures(double surfaces, double perimeter) {

    super(); //**<---Why use super call here?**

    this.surfaces= surfaces;
    this.perimeter= perimeter;
}

public double getSurfaces() {
    return surfaces;
}

public double getPerimetre() {
    return perimeter;
}


}

So basically everything is in the title. I want to know why i'd need to use the super() in a super class.

Thx for the help.

marcAndreG
  • 161
  • 1
  • 1
  • 10

5 Answers5

5

In this case super() refers to the constructor of Object, which is every class' Superclass.

You don't actually need to call it though, since by default a constructor always tries to call super() if no other superclass constructor is called.

I've seen some code generators add it automatically, my guess is that it is considered a best practice as an explicit reminder that super() will be called.

ecarlos
  • 209
  • 1
  • 6
2

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.

according to docs.oracle

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1

You don't have to, because it will be added implicitly for you by the compiler.

I think IDEs add it automatically for you because they generate code based on templates. This could be helpful if super constructor requires some arguments that could be extracted locally.

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

Imagine that you have this class:

public class A {
   public A() {
       // N lines of code
   }
}

And then you have two child classes:

public class B extends A {
   public B() {
       // N lines of code
       // M lines of specific code for class B
   }
}

public class C extends A {
   public C() {
       // N lines of code
       // P lines of specific code for class C
   }
}

As you can note, since B and C classes inherit from A, you have to inherit the code of the constructor too. So, if you couldn't use super(), you would have to copy-paste all the code in every child class. And so on...

It also implies a certain level of abstraction when you extend some class, since the superclass may have certain code in its constructor, such as initializations.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

In your example, there is no reason to use super(). Java will automatically make that call for you even if you don't explicitly write it.

So when do you need to call super()? Here's an example, using a subclass of your GeometryFigures class.

public class MyFigure extends GeometryFigures {

    public MyFigure() {
        super( 5.0, 42.0 );
    }

    // ...
}

Here you need a call to super() because there is no default (no argument) constructor in your class. Java will not supply a call to super() that has arguments (how could it? What parameters would it use?). So in this case you must supply a call, and specify which constructor and which parameters to use.

markspace
  • 10,621
  • 3
  • 25
  • 39