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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

The third answer is correct. A constructor is defined in B, so there are no problems

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

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

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

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

So, if a super class does not have a no argument constructor, sub class could not have a no argument constructor?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

bsterhiha
Posts: 2
Joined: Thu May 08, 2014 9:34 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Why would it need super class's constructor? B doesn't extend A in this option.

bsterhiha
Posts: 2
Joined: Thu May 08, 2014 9:34 am
Contact:

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

Post by bsterhiha »

oops It's my fault. Actually B doesn't extends A. option 4 is OK ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests