wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes superclass no-arg constructor... mock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "superclass no-arg constructor... mock question" Watch "superclass no-arg constructor... mock question" New topic
Author

superclass no-arg constructor... mock question

Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
Hi friends,

i dont understand why this code compiles properly?

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
}
public class C extends B
{
private C(){ super(); } // C1
public C(String s){ this(); System.out.println("C :"+s); } // C2
public C(int i){} // C3

public static void main(String[] args) {
C c = new C();
}
}


Since there is no no-arg constuctor in B ( we need to type it in because we have defined ome other constructor) , wont calling the super() from line C1 cause a compiler error?? what am i missing here?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Radha,

I guess, you must have got confused with the name of the method in B. If you see, there is no construtor defined in class B and hence compiler provides a default constructor. In fact at the very first glance even I thought like you and even executed it keep a println statement in no arg construtor A . This is one of the serious traps in SCJP exam, which we should be care enough.
Mohammad Hossain
Greenhorn

Joined: Jul 26, 2007
Posts: 14
public int B(String s)

This is not a constructor. This is a method, having the same name as constructor. Because constructors have no return types.


Live Free Or Die
Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
yes... how careless of me...

Thanks friends...
 
Don't get me started about those stupid light bulbs.
 
subject: superclass no-arg constructor... mock question
 
Similar Threads
Can you please clarify my doubt ?
sample experimanting mock code(consturctor related)
runtime and compile time error case confusion
Constructors
Constructor question