File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

John Hunt M.E Q51

 
Vineet Sharma
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Explain:
class First{
public First (String s) {
System.out.println(s);
}
}
public class Second extends First {
public static void main(String args[] ) {

new Second();
}
}
A.Nothing happens
B.A string is printed to Standard Output
C.An exception is raised at runtime stating that there is no null parameter constructor in class First.
D.Runtime error is raised stating that the object has not been created.
E. class second will not compile as there is no null parameter constuctor in the class First.
Answer is E but I think it should be D. Because no object has been created.
Thanks
 
Pat Barrett
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Actually, the line new Second(); is creating an object. Since Second is extending First, and since Second doesn't have an explicit default constructor, the lack of a default constructor in First will cause a compiler error.
Hope this helps,
Pat B.
 
Vineet Sharma
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Pat,
But is it not true that if we do not make a constructor,a default constructor is issued?
Regards
Vineet
 
Pat Barrett
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is true... but since the "First" method has a constructor defined, you would need to create your own default, or zero arg constructor in "Second" if you wanted one used there.
Pat B.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic