aspose file tools*
The moose likes Mock Exam Errata 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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "default constructor" Watch "default constructor" New topic
Author

default constructor

jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default constructor