3

What does the code

super();

do inside of a constructor?

For example, here is a constructor for my class

public abstract class Rectangle extends AbstractShape
{

private double height, width;

// Constructors...  
public Rectangle()
{
    super(); //this is how i inherit that point!
    height = -1;
    width = -1;
}

Does it have something to do with super-classes?

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
macschmidt33
  • 105
  • 1
  • 1
  • 9
  • 4
    Question was well stated, however. So, I am surprised he now has a negative 7 vote on the question. – finneycanhelp May 01 '13 at 16:27
  • @finneycanhelp: As at:Nirbhay already mentioned, it would have been easy to find an answer to this question by just doing a bit of research (googling)... IMO this is the reason for the downvotes. – home May 01 '13 at 16:34
  • ok. So, in the future the google keywords to use are: super java constructor – finneycanhelp May 01 '13 at 20:21

4 Answers4

4

It calls the parent class's constructor

KyleM
  • 4,445
  • 9
  • 46
  • 78
  • Is using super class a self declaration even if it is not added in the initialization? – David Dimalanta Oct 09 '14 at 02:22
  • 1
    @DavidDimalanta "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." http://docs.oracle.com/javase/tutorial/java/IandI/super.html – KyleM Oct 09 '14 at 14:45
3

It goes to the parent class and calls the constructor.

This link over at oracle should help

g-aldrighetti
  • 91
  • 1
  • 5
1

The call super(); explicitly invokes the superclass constructor, here the parameterless constructor for AbstractShape. If this call is not present in a constructor, then Java inserts an implicit super(); for you.

Quoting the JLS, section 8.8.7:

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

It calls the constructor of the superclass. See the following questions:

Community
  • 1
  • 1
Matthias Herlitzius
  • 3,365
  • 2
  • 20
  • 20