About Question enthuware.ocajp.i.v7.2.1208 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1208 :
The third answer is correct. A constructor is defined in B, so there are no problems
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
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.
HTH,
Paul.
-
- Posts: 18
- Joined: Mon Jan 13, 2014 5:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
ETS User, I thought so at first too, but try coding it up.
For example:
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.
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();
}
}
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
So, if a super class does not have a no argument constructor, sub class could not have a no argument constructor?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
No, that is not what it says. Subclass can still have a no args constructor like this:fasty23 wrote:So, if a super class does not have a no argument constructor, sub class could not have a no argument constructor?
Subclass(){
super(pass some parameter here); //explicitly invoke available constructor of superclass
}
-
- Posts: 2
- Joined: Thu May 08, 2014 9:34 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
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
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
Why would it need super class's constructor? B doesn't extend A in this option.
-
- Posts: 2
- Joined: Thu May 08, 2014 9:34 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1208 :
oops It's my fault. Actually B doesn't extends A. option 4 is OK 

Who is online
Users browsing this forum: No registered users and 11 guests