wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor 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 "constructor" Watch "constructor" New topic
Author

constructor

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
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?
The answer is:
Public SuperClass(int i)
Public SuperClass();
But, Why SuperClass()?? When will be called ??
Thank you in advanced.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
SuperClass will be called when the first constructor of SubClass is invoked because the body of that constructor does not specify which one of the SuperClass constructor will be called so the default constructor gets called
Val
[This message has been edited by Valentin Crettaz (edited September 20, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
But constructor of SubClass will be invoked SubClass(int)
???
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
SubClass extends SuperClass, as a result every constructor of SubClass that will be invoked will have as the first statement in its body a call to a constructor of SuperClass.
Now, if you look at the following line,
public SubClass( int m, int n ) { i = m ; j = m ; } //1
you can see that no ctor is explicitely written, the compiler will add a call to the default SuperClass ctor, which is SuperClass().
The compiler will modify the code like this
public SubClass( int m, int n ) { super();i = m ; j = m ; } //1
The next line
public SubClass( int m ) { super(m ); } //2
provides explicitely a call the SuperClass ctor taking an int as argument so the ctor SuperClass(int i) is needed in the SuperClass too.
val
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Every constructor has an implicit call to its superclass' constructor. It is the first call in a constructor. In the code you posted...super(m) implies that the constructor has been overloaded in the base class. The rule is...If the constructor is overloaded, there'll be no implicit default constructor available. Therefore, the super class needs to explicitly declare a default constructor because in the call... public SubClass( int m, int n ) { super(); i = m ; j = m ; } , the compiler will insert an implicit call to the superclass' default construtor(shown in red). If there is not one...it'll flag an error.
So the constuctors SuperClass(); and SuperClass(int) are necessary here.
Hope this helps
Shyam


[This message has been edited by Shyamsundar Gururaj (edited September 20, 2001).]
 
 
subject: constructor