• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors JQ+

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID :957621799662
Consider the following subclass definition:
public class SubClass extends SuperClass
{
int i, j, k;
public SubClass( int m, int n ) { i = m ; j = m ; } //1
public SubClass( int m ) { super(m ); } //2
}
Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?
answer is:
public SuperClass(int a)
public SuperClass()
I missed the no args constructor. So all Classes have to have a no args constructor?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not all constructors have will have a no-args constructor.
If no constructors are available for the class, one will be created.
We can also create a no-args constructor.
Keep in mind the automatic call to the no args parent constructor if we do not explicitly use super or this.
Hope this helps.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
Please note that JVM supplies a default constructor (i.e., zero
argument constructor) ONLY if you do define ANY constructor.
But this provision is surrendered the moment you define even
one argument constructor. Now, when you create a instance of
subclass the default constructor of super class is implicitely
called by a call to "super()", in such a scenario the
superclass MUST have a default constructor else the compiler
raises a compile time error.
Hope this clears the issue.
Ravindra Mohan.
 
Andy Steele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your posts. I am still a bit confused I am afraid. So when I instantiate SubClass like SubClass mySubClass = new SubClass(5), both the no args constructer and the 1 args constructer gets called in the SuperClass? Thanks for the Help!
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,
Only ONE Constructor is called per object.
Take a look at the constructors again. You have to of them.
public SubClass( int m, int n ) { i = m ; j = m ; } //1
public SubClass( int m ) { super(m ); } //2
}
The first calls the default constructor in its parent - class.
The second calls an constructor with an int as parameter (super(m).
So you need 2 constructors in the parent class.
But only one will be called PER OBJECT.

If you create an Subclass - object with new SubClass(int, int) this Constructor will invoke an no-argument constructor in the parent-class.

If you create an Subclass - object with new SubClass(int) this Constructor will invoke an super(int)-Constructor in the parent class.
You may try this out, writing code. You can place System.out.println("no-argument-constructor-parent called"); in the constructors.
Convinced that your confusion will get smaller with the time.
Axel
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is really based on 2 simple concepts :
1. If class's constructor does not call any of the super class's constructor explicitly then the compiler will automatically insert super(); as the first line of that constructor. (meaning a call to no args constructor of the super class)
2. Default (no args) contrustructor is automatically generated by the compiler iff the programmer does not define ANY CONTRUCTOR AT ALL.
Now, to satisfy
public SubClass( int m ) { super(m ); } //2
you need: public SuperClass(int a) in SuperClass.
But the moment you put a constructor explicitly, the compiler will not generate the default constructor (Because of rule 2 above)for the super class . So
public SubClass( int m, int n ) { i = m ; j = m ; } //1
will fail because of rule 1.
HTH,
Paul.


------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Andy Steele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand now! Thanks for all the replys.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic