aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q. about constructors (and inheritage) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q. about constructors (and inheritage)" Watch "Q. about constructors (and inheritage)" New topic
Author

Q. about constructors (and inheritage)

Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
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 ]
Abu Yoosuf
Ranch Hand

Joined: Nov 14, 2002
Posts: 33
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

Joined: Oct 23, 2002
Posts: 904
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
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
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.


SCJP 1.4
Abu Yoosuf
Ranch Hand

Joined: Nov 14, 2002
Posts: 33
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q. about constructors (and inheritage)