• 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

Q. about constructors (and inheritage)

 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya folks!
This is a question about constructors in classes that are extended. I just want to be sure, that I've understood the subject.
During this post I will primarily be using two classes: SuperClass and SubClass (which extends SuperClass). Both classes are placed in a file called SubclassConstructor.
1. When I call SubClass s = new SubClass() the constructor of Superclass will be called first and at last SubClass' constructor is called.
For the fun of it I make this example:

The output is:
superclass constructor!
Sub class' constructor!
This is SubClass2
But! - and here comes my question:
If I place the following constructor in SuperClass: public SuperClass(String s) and try to initialize SubClass from the main method (as in the following example) with this line: SubClass sc = new SubClass("test"); then I get a compiler error.. why ? can anyone help here?

Is it because you can't inherit the constructor ? (I dont think so - cause the constructor of SuperClass was called in the 1. example).
Thanks in advance
/Svend Rost
ps. I edited a few typos in the text.
[ December 02, 2002: Message edited by: Svend Rost ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your SubClass source doesn't have a definition for SubClass(String str){}. So the compiler complains when you call new SubClass("something");
As for the inheritance, Constructors aren't inherited.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer Abu
Yes the SubClass doesn't have a constructor taking a string as an argument.. but I guessed it was ok since the superclass' constructor gets called first.
/Svend Rost
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Svend Rost:
Thanks for the answer Abu
Yes the SubClass doesn't have a constructor taking a string as an argument.. but I guessed it was ok since the superclass' constructor gets called first.
/Svend Rost


Svend
That statement is not quite correct - the SubClass constructor is invoked first when a SubClass object is created, not the super-class's constructor. The first thing the SubClass constructor will do when executed is invoke the default super class constructor via super(); (unless you explictly coded a call to this() or super() as the first line in the SubClass ctor).
See JLS section 8.8.5, "Constructor body" for a more complete explanation.
 
Abu Yoosuf
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a clear difference in calling the
and
In the first example, the SubClass and the SuperClass have their default constructors.
In the second example, the SuperClass has SuperClass(String str) in addition to its default constructor.
So, when the SubClass is constructed its parent hierarchy can be initialized in both ways. (1) calling super(); (2) super("something");. If you don't specify a constructor for the parent within the child constructor (it has to be the first call within the child constructor, if you make a specific call) the default contructor of the parent is used (provided the parent doesn't have other constructors).
The default constructor for SubClass (in the first example) is similar to the following code snippet.

In the second example, if you have left out the String parameter to the constructor, you would have still seen the same output you saw for the first example. That is because SubClass() call makes an implicit call to super();
[ December 02, 2002: Message edited by: Abu Yoosuf ]
reply
    Bookmark Topic Watch Topic
  • New Topic