This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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 ]
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.
Joined: Nov 14, 2002
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 ]