• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Constructor

 
Deepak Jindal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Priya.
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer should be F. not E. It won't compile since the superclass does not have a default constructor.
thanx.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is indeed F, a compile time error as Priya has already pointed out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic