Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Mon Jan 14, 2013 6:23 am
by ETS User
The third answer is correct. A constructor is defined in B, so there are no problems

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Mon Jan 14, 2013 7:05 am
by admin
No, it is not correct. It has the same problem as option 2. In case of option 2, there is no constructor provided explicitly so the compiler provides one implicitly. The one that it provides has the same signature as the constructor specified in option 3. So basically, option 2 and 3 are same.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Wed Jan 15, 2014 3:38 am
by kecker
ETS User, I thought so at first too, but try coding it up.

For example:

Code: Select all

class A{
  protected int i;
  A(int i) {
    this.i = i;
    System.out.println("A i");
  }

}

class B extends A{

  B(){
    System.out.println("B");
  }

  public static void main(String args[]){
    System.out.println("Starting Main");

    B b = new B();
  }
}
You'll get an error, but just add a simple A(){} to the A class and it works. I needs that no-arg constructor, unless you explicitly call super(1); or something like that.

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Fri Feb 14, 2014 10:45 am
by fasty23
So, if a super class does not have a no argument constructor, sub class could not have a no argument constructor?

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Fri Feb 14, 2014 10:55 am
by admin
fasty23 wrote:So, if a super class does not have a no argument constructor, sub class could not have a no argument constructor?
No, that is not what it says. Subclass can still have a no args constructor like this:
Subclass(){
super(pass some parameter here); //explicitly invoke available constructor of superclass
}

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Fri May 09, 2014 2:39 am
by bsterhiha
Option 4 : class B { B() {} } is said to be correct but it doesn't compile. Because implicit super constructor A() is undefined. So there is only one option possible

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Fri May 09, 2014 3:52 am
by admin
Why would it need super class's constructor? B doesn't extend A in this option.

Re: About Question enthuware.ocajp.i.v7.2.1208 :

Posted: Fri May 09, 2014 8:29 am
by bsterhiha
oops It's my fault. Actually B doesn't extends A. option 4 is OK ;)