0

Could you please help in understanding the output of below java code where this(0) of Superclass is called when object is created for Subclass.

In this code though there is no mention in the code to explicitly print 20, why the first value is printed as 20?

class Superclass {
    Superclass() {
        this(0);
        System.out.println("1");
    }

    Superclass(int x) {
        System.out.println("2" + x);
    }
}

class Subclass extends Superclass {
    Subclass(int x) {
        System.out.println("3" + x);
    }

    Subclass(int x, int y) {
        System.out.println("4" + x + y);
    }
}

public class practiceTest {
    public static void main(String[] args) {
        Subclass s = new Subclass(10, 20);
    }
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Hari
  • 17
  • 2
  • 2
    "why the first value is printed as `"20"`" -> because you call `Subclass`'s `(int, int)` constructor. Within this constructor, you have an (implicit) call to `Superclass`'s no-args constructor. Within `Superclass`'s no-args constructor, you call `Superclass`'s `(int)`-constructor and there you print a `"2"`, followed by `x`'s value, which is `0`, producing the output `"20"`. Try stepping through the code using a debugger. – Turing85 Dec 09 '17 at 13:11
  • I don't get the down-votes. OP gave everything we need and explained the problem. Maybe it's because of *lack of research* but it can be hard to research if you don't know for what. – Zabuzard Dec 09 '17 at 16:05
  • Besides the inheritance and object creation there is the String + operator question... So... not exactly a duplicate IMO – Lucas Oliveira Dec 09 '17 at 16:12

2 Answers2

2

String addition

In this code though there is no mention in the code to explicitly print "20"

Actually there is, take a look at your Superclass

Superclass() {
    this(0);
    System.out.println("1");
}

Superclass(int x) {
    System.out.println("2" + x);
}

Addition of Strings is interpreted as concatenation, not as ordinary addition (of values). So calling the default constructor of Superclass triggers this(0) which yields

    System.out.println("2" + x);
=>  System.out.println("2" + 0);
=>  System.out.println("20");

thus printing 20.


Integer addition

If you want to add the values as integer you need to use int values and not String values, for example like

Superclass(int x) {
    System.out.println(2 + x);
}

then you will also get

    System.out.println(2 + x);
=>  System.out.println(2 + 0);
=>  System.out.println(2);

Constructor-chain

Okay, so what happens exactly if you call

Subclass s = new Subclass(10, 20);

Well, first, of course, you trigger this constructor

Subclass(int x, int y) {
    System.out.println("4" + x + y);
}

Now in Java, when creating sub-classes you will always need to call one of the constructors of the super-class (it's per definition of the language).

And if you don't explicitly specify such a call then it tries to call the default constructor of your super-class. If there is no, it won't compile. However you have such a default constructor, the code will thus be equivalent to

Subclass(int x, int y) {
    // Implicit call of default super-constructor
    super();
    System.out.println("4" + x + y);
}

So you call the following constructor

Superclass() {
    this(0);
    System.out.println("1");
}

which, as shown above, triggers the other constructor

Superclass(int x) {
    System.out.println("2" + x);
}

So what you will get, if resolving this chain, are the following outputs

// from Superclass(int x)
System.out.println("2" + 0);
// from Superclass()
System.out.println("1");
// from Subclass(int x, int y)
System.out.println("4" + 10 + 20);

which yields

20
1
41020
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
0

Whenever a constructor is invoked the first statement needs to be a call to any constructor of the superclass. If you don't call it explicitly, the compiler will add a call to the default constructor for you (As explained in this question.).

In your case, the default constructor of your superclass calls the other constructor which in turn calls System.out.println("2" + x);

This statement will print the string "2" appended to Integer.valueOf(x).toString(), resulting on the String "20" in your case (because x == 0).

Lucas Oliveira
  • 3,357
  • 1
  • 16
  • 20