This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Since the SubClassB constructor (with the String argument) will not automatically call the SuperClassA constructor (with the String argument), the compiler will "insert" a "super()" in the SubClassB constructor. But the noargs-constructor does not exist in SuperClassA. So the compilation will fail. Solution: Either implement a no-args-constructor in SuperClassA or perform an explicit call to the one-args-constructor of SuperClassA (i.e. "super(identity)" )in the first line of the SubClassB-constructor.
Joined: Aug 25, 2005
Thanks Anton ! I was thinking if i had written a no-argument constructor in the sub-class then the compiler would insert a no-argument call to super class constructor, then SIMILARLY the PARAMTERIZED constructor in subclass would also insert a call to PARAMETERIZED constructor in super class ! .... but i think as per what you said my understanding was WRONG ! Thanks !
But, what possible harm it would make if compiler inserts an implicit call to PARAMETERIZED constructor in the superclass from PARAMETERIZED construtor of subclass ...i.e.,
OR why does the compiler inserts a call to no-argument constructor in the superclass ? ...why it can't be just :-
Joined: Jan 10, 2007
1st way: (does not compile!!)
A is a subclass of Object. If it would be done the way you suggested, the compiler would add the "super(str)". But the class Object doesn't have the constructor Object(String a). So, compilation would fail.
If there would not be an implicit call to the noargs-constructor of A, (remember, B is-a A)where does B's x (inherited) come from? Who will construct the A-part of B?