Page 1 of 1

About Question com.enthuware.ets.scjp.v6. 2 . 531 :

Posted: Sat May 30, 2015 4:22 pm
by pushpull
Why the answer
One of the constructors of each class is called as a result of constructing an object of class C.
is correct?
The code is:

Code: Select all

class A
{
  public A() {} // A1
  public A(String s) {  this();  System.out.println("A :"+s);  }  // A2
}
class B extends A
{
  public int B(String s) {  System.out.println("B :"+s);  return 0; } // B1
}
class C extends B
{
    private C(){ super(); } // C1
    public C(String s){  this();  System.out.println("C :"+s);  } // C2
    public C(int i){} // C3
}
So while constructing an object of class C, it would try to invoke the default constructor of class B, which is absent, so it can't be called, wherefore there will never be a call to the constructor of class A.

Re: About Question com.enthuware.ets.scjp.v6.2.531 :

Posted: Mon Jun 01, 2015 10:41 am
by admin
pushpull wrote:So while constructing an object of class C, it would try to invoke the default constructor of class B, which is absent, so it can't be called, wherefore there will never be a call to the constructor of class A.
Why do you think that while constructing an object of class C, it would try to invoke the default constructor of B?

Re: About Question com.enthuware.ets.scjp.v6.2.531 :

Posted: Mon Jun 01, 2015 11:14 am
by pushpull
Because it extends class B? And B extend A?

Re: About Question com.enthuware.ets.scjp.v6.2.531 :

Posted: Mon Jun 01, 2015 12:03 pm
by admin
That doesn't answer why you think it would try to invoke the default constructor of B. It only answers that creating an instance of C would cause a constructor of B to be invoked.

Re: About Question com.enthuware.ets.scjp.v6.2.531 :

Posted: Mon Jun 01, 2015 1:53 pm
by pushpull
Mayby I'll quote the Katty Perry SCJP book. Page 143:
Key Rule: The first line in a constructor must be a call to super() or a call to
this().

No exceptions. If you have neither of those calls in your constructor, the compiler
will insert the no-arg call to super(). In other words, if constructor A() has a call
to this(), the compiler knows that constructor A() will not be the one to invoke
super().
The preceding rule means a constructor can never have both a call to super()
and a call to this(). Because each of those calls must be the first statement in a
constructor, you can't legally use both in the same constructor. That also means the
compiler will not put a call to super() in any constructor that has a call to this().
Moreover, here we have a direct call to super() in C(), and call to this() in C(String s). So eventually, super() will directly happen. In C(int i) we have nothing, so as I quoted: "the compiler will insert the no-arg call to super()".

Re: About Question com.enthuware.ets.scjp.v6.2.531 :

Posted: Mon Jun 01, 2015 8:30 pm
by admin
Ok, I got confused with this question as well :oops:
Now, that I have thoroughly investigated the options and the given explanation, I see that there is nothing wrong with the question. You are right that default constructor of B will be invoked. But you are wrong that B does not have a default constructor. Since there is no explicitly defined constructor in B, the compiler will provide one automatically.

HTH,
Paul.