This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default Constructor" Watch "Default Constructor" New topic
Author

Default Constructor

Deepak Jindal
Ranch Hand

Joined: Nov 16, 2000
Posts: 30
class First {
public First (String s) {
System.out.println(s);
}
}
public class Second extends First {
public static void main(String args []) {
new Second();
}
}
What is the result of compiling and running the Second class?
A.Nothing happens
B.A string is printed to the standard out
C.An instance of the class First is generated
D.An instance of the class Second is created
E.An exception is raised at runtime stating that there is no null parameter constructor in class First.
F.The class second will not compile as there is no null parameter constructor in the class First
Select the most appropriate answer.
The answer is E. My argument is: won't the default constructor be created by JVM.
Priya Kannan
Ranch Hand

Joined: Jan 19, 2001
Posts: 72
Hi Deepak,
A default constructor can be created in the Second class ,but when the implicit super() is fired from the Second's default constructor there is no matching constructor in the class First!.
Also a default constructor is created in the class First only if there's no other constructor, defined in that class.But First has already got a constructor and that means...A compile time error!
Deepak Jindal
Ranch Hand

Joined: Nov 16, 2000
Posts: 30
Thanks Priya.
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
The answer should be F. not E. It won't compile since the superclass does not have a default constructor.
thanx.

Sun Certified Programmer for Java 2 Platform
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
The answer is indeed F, a compile time error as Priya has already pointed out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default Constructor