Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But constructor of SubClass will be invoked SubClass(int)
???
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic