File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default constructor

 
jordan gong
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Helper:
According to the theory that, "unless a different superclass constructor is requested", the no-arguement/default constructor of the superclass is always implicitly called at the beginning of a class's constructor. The "class DerivedDemo" below requests a different constructor of the superclass "public DerivedDemo( int x ){super( x );}, then why the superclass's default constructor should still be called?
Thanks and look forward to your instruction.

> ================ Here is the question text
> The compiler puts in a call to the no-args
> constructor
> right after line 3. This is default behavior by
> Java.
>
> Question 11 Select all which are correct
> Category is default constructor
>
> Given the following class definition:
> 1. public class DerivedDemo extends Demo{
> 2. int M, N, L ;
> 3. public DerivedDemo( int x, int y ){
> 4. M = x ; N = y ;
> 5. }
> 6. public DerivedDemo( int x ){
> 7. super( x );
> 8. }
> 9. }
> Which of the following constructor signatures MUST
> exist in the Demo
> class for DerivedDemo to compile correctly?
>
> -- options --
> a - public Demo( int a, int b )
>
> b - public Demo( int c )
>
> c - public Demo( )
>
>
> --- discussion, * marks correct answer(s)
>
> a - Option a is not required because no
> constructor with that
> signature is explicitly called.
>
> * b - Option b is required because it is called in
> line 7.
>
> * c - Option c is required because a default (no
> arguments)
> constructor is needed to compile the constructor
> starting in line 3.
> (Many people missed this option.)
>
>
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jordan,
Before a base class can be fully initizialized, all of it's superclasses must be initialized. The DerivedDemo ctor defined at line 3 does not explicitly call a superclass ctor so the compiler calls the default no-arg ctor in Demo(). If there is no default ctor, DerivedDemo() will not compile.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic