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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance Question" Watch "Inheritance Question" New topic
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: 977
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. - What truth? - That there is no spoon!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Question