5

The following code (shortened version of the class) I have seen in a Java Video-Tutorial.

public class Address {
    private String firstName;
    private String lastName;

    public Address() {
        super();
    }

    public Address(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    ...
}

The class which is shown there has no extend keyword. Nevertheless super() is used within the constructor without a parameter.

It calls the constructor of Object class? Am I right?

But why?

What is the reason for having this call of super()?

Lrrr
  • 4,755
  • 5
  • 41
  • 63
cluster1
  • 4,968
  • 6
  • 32
  • 49
  • 4
    You're right, and there's no reason. The constructor of `Object` class will be called anyway. – Eran Jun 04 '17 at 09:16
  • 3
    Whether or not the class has a superclass other than Object, an explicit call to `super()` is always redundant, because the compiler will add one if you don't, as the first instruction to all of the constructors. The posted code is inconsistent, since it uses super() on one constructor, but not in the other one. I'm not surprised to find that kind of ugly code in a video tutorial. Youtube is for lolcats. Free video tutorials are most of the time of very poor quality. – JB Nizet Jun 04 '17 at 09:17
  • 2
    Every class has a super-class if not explicitly specified: Object – Mark Rotteveel Jun 04 '17 at 09:18
  • Most probably, the author of youtube tutorial used some default setting for IDE code generation. Usually super() is just omitted. – dvelopp Jun 04 '17 at 09:36
  • @dvelopp Yep. I guess that's really the reason why it occurs in there. It's like I supposed and the answers confirm: There's no deeper reason. – cluster1 Jun 04 '17 at 09:38

2 Answers2

4

that is what you get when you let your IDE to autogenerate the Constructors of your class.. since that class is not extending anything then calling super is the same as calling the constructor of the object class, which is actually taking no effects on the Address class implementation

so in your case

public Address() {
    super();
}

is the same as

public Address() {

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
4

Constructors of superclasses are invoked when you create an instance of your class. In case your class extends sth explicitly, there are two options:

  • If the super class has a default constructor, then it's invoked;
  • In case there is no default constructor, you should specify which constructor you invoke and send corresponding arguments for it.

Calling super() without parameters basically means that you invoke a default constructor. It's not necessary, because it will be invoked even if you don't do it explicitly.

Since all the objects in java implicitly extend Object, then the default constructor of Object is invoked all the time you create an instance of the class. super() just makes it explicit. But, why should we add explicitness to something that is quite clear already.

Moreover, that's sometimes added by IDE when you use code generation tools(e.g. Eclipse).

You can read really good description on how consturtors of class/superclass work in a book for OCA preparation: https://www.amazon.com/OCA-Certified-Associate-Programmer-1Z0-808/dp/1118957407

dvelopp
  • 4,095
  • 3
  • 31
  • 57