This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

default constructor

 
Ranch Hand
Posts: 37
  • 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.)
>
>
 
Ranch Hand
Posts: 3141
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!