This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors JQ+

 
Andy Steele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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?
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • 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.
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • 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
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3752
10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I understand now! Thanks for all the replys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic