• 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

constructor

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But constructor of SubClass will be invoked SubClass(int)
???
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic