| Author |
Inheritance Question
|
Swati Singhal
Ranch Hand
Joined: Dec 08, 2003
Posts: 31
|
|
Hi, I am a lil confused with this question. Appreciate if somebody could clarify. Which of the following constructors must exist in Parent class? I thought the answer would be c, but it is both a & c.
|
 |
Gian Franco
blacksmith
Ranch Hand
Joined: Dec 16, 2003
Posts: 975
|
|
Hi Swati, Given the two constructor in the Child class both a and c must be added, because for the latter an explicit call is done with super(i,j). The former is called by the default no-argument constructor that JVM inserts in the Child(int i) constructor as the first line of code. You have to add the no-argument constructor actually because you just had to add public Parent(int i, int j). Normally the JVM creates a default no-argument constructor for you, but with one condition: there must NOT be any other constructor. HTH, Gian Franco
|
"Eppur si muove!"
|
 |
Richard Quist
Ranch Hand
Joined: Feb 18, 2004
Posts: 96
|
|
Originally posted by Swati Singhal: Hi, I am a lil confused with this question. Appreciate if somebody could clarify. Which of the following constructors must exist in Parent class? I thought the answer would be c, but it is both a & c.
You need both because the Child constructor public Child(int i){ } has an implicit call to the compiler-provided, default, no-arg constructor of the parent (answer A). Answer C is needed because of the explicit call to super(i,j); in the Child constructor public Child(int i, int j) Also note that the method public void Child(int i, int j, int k) is NOT a constructor - since constructors don't have return types (and this method is declared to return void) Hope this helps
|
Rich
SCJP 1.4
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
The compiler will do an implicit call to the no-args constructor in the super class parent. That is why this code doesn't compile, to allow the compilation, you must provide a no argument constructor in the super class, see code:
|
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
|
 |
 |
|
|
subject: Inheritance Question
|
|
|