2

I'm trying to understand Java super() constructor. Let's take a look in the following class:

class Point {
    private int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
        this(0, 0);
    }
}

This class will compile. If we create a new Point object say Point a = new Point(); The constructor with no parameters will be called: Point().

Correct me if I'm wrong, before doing this(0,0), the Class constructor will be called and only then Point(0,0) will be called. If this is true, is it correct to say that super() is being called by default?

Now let's look at the same code with a small change:

class Point {

        private int x, y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public Point() {
            super();   // this is the change.
            this(0, 0);
        }
    }

Now, the code won't compile because this(0,0) is not in the first line of the constructor. This is where I get confused. Why the code won't compile? Doesn't super() is being called anyway? (The Class constructor as described above).

DifferentPulses
  • 404
  • 2
  • 17

5 Answers5

5

this(0, 0); will call, constructor of the same class and super() will call super/parent class of Point class.

Now, the code won't compile because this(0,0) is not in the first line of the constructor. This is where I get confused. Why the code won't compile? Doesn't super() is being called anyway?

super() must be the first line of the constructor and also this() must be the first line in the constructor. So both cannot be in the first line(not possible), that means, we cannot add both in a constructor.

As a explonation about your code:

this(0, 0); will call to the constructor that takes two parameters(in Point class) and that two parameter constructor will call to the super() implicitly(because you didnt call it explicitly).

Blasanka
  • 21,001
  • 12
  • 102
  • 104
4

Put super() in your first constructor, it should work.

class Point {

        private int x, y;

        public Point(int x, int y) {
            super();   // this is the change.
            this.x = x;
            this.y = y;
        }

        public Point() {
            
            this(0, 0);
        }
    }
Zhang Bruce
  • 884
  • 1
  • 8
  • 23
4

You can call this() or super() but not both from the same constructor. When you call this(), super() is called automatically from the other constructor (the one that you call with this()).

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

By calling this(...) you are forced to call the super constructor (if present) in the constructor you're calling there.

Calling this(...) or super(...) is always the first method you're calling in a constructor.

EDIT

class Point {
    private int x, y;

    public Point(int x, int y) {
        super(...); //would work here
        this.x = x;
        this.y = y;
    }

    public Point() {
        this(0, 0); //but this(...) must be first in a constructor if you want to call another constructor
    }
}
Cedric
  • 532
  • 5
  • 7
  • This is exactly my point. If i'm forced to call the super constructor, what difference does it make if I call it explicitly by super() and then call this(..)? – DifferentPulses Jul 24 '17 at 19:38
  • 1
    You are not allowed to call super/this as a second method. If you want to call a super constructor, you need to do it in the constructor you're calling. – Cedric Jul 24 '17 at 19:42
1

A constructor can call the constructor of a super class using the super() method call. Only constraint is that it should be the first statement.

public Animal() { 
    super();
    this.name = "Default Name"; 
}

Will this code Compile?

public Animal() {
    this.name = "Default Name"; 
    super();
}

Answer is NO. super() should be always called on the first line of the constructor.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108