aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors JQ+ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors JQ+" Watch "Constructors JQ+" New topic
Author

Constructors JQ+

Andy Steele
Greenhorn

Joined: Jun 06, 2001
Posts: 6
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

Joined: Jan 29, 2001
Posts: 213
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

Joined: Mar 16, 2001
Posts: 216
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

Joined: Jun 06, 2001
Posts: 6
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

Joined: Jan 08, 2001
Posts: 2164
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

Joined: Sep 23, 2000
Posts: 3312
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Andy Steele
Greenhorn

Joined: Jun 06, 2001
Posts: 6
I understand now! Thanks for all the replys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors JQ+