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).

5
  • Point is not a subclass. Imagine if we had a subclass PrettyPoint, then PrettyPoint() { super(0,0); } would call Point(int x, int y) from above. Commented Jul 24, 2017 at 19:33
  • 1
    This will help you to understand: stackoverflow.com/questions/10381244/… Commented Jul 24, 2017 at 19:42
  • @ZeldaZach Everything is a subclass of Object. Commented Jul 24, 2017 at 19:42
  • @Rom What is the compiler error in the second version of your code? Commented Jul 24, 2017 at 19:43
  • Duplicate of stackoverflow.com/questions/1168345/… Commented Jul 24, 2017 at 19:58

5 Answers 5

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).

Sign up to request clarification or add additional context in comments.

Comments

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()).

2 Comments

And therefore I have 2x calls for super(). Thank you.
@Rom If the code could compile, that would be one of the results, yes.
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);
        }
    }

Comments

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
    }
}

2 Comments

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(..)?
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.