File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inheriting constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheriting constructors" Watch "inheriting constructors" New topic
Author

inheriting constructors

Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
I am really confused after studying too much abt constuctors and overriding. Especially, on one of Dan's Exams there is a question as follows
(Dan !! i hope its ok to post it here without being sued for copyright infringement or something like that!!!)

The answer given to the question is
f.Compiler error.
Will somebody please explain this and also the rules
Thanks a lot
Sri
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
Hey Sri,
Here class U extends class T and we r trying to create an instance using non default constructor.
T t = new U(1,2);
If an explicit call to the super class constructor is not made in the first line of the subclass construstor using super, then the sub class constructor calls the super class default constructor.Here we don't have a super class default constructor and any attempt to call it gives the compiler error.
To make this prog work properly, either create a no parameter constructor in class T or insert super(1,2) in the first line of the subclass constructor
I hope u could understand..
basanti
[ January 13, 2003: Message edited by: Basanti Mathad ]
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Basanthi,
Thanks for the reply....Just to make sure I understood what you wrote, let me write something.
Because we are trying to instantiate a child object with a base class reference variable, the base class constructor comes into picture. If we are going to instantiate a Child object with a child class reference variable , then the compiler doesnt complain.
Thanks in advance for your reply
Sri
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Sri Sri,
First of all, constructor is NOT inherited from the parent class. (you need define them OR compiler provide a no-argument constructor if not even one given in your code).
Secondary, creating a object require all upper hierarchy class being inialized/constructed.
<- which means in the child constructor, compiler will implicitly insert a super() at the first line if you does not explicity call the parent
constructor.

Combine above two rules, as you are creating a child object and you does not provide any code for parent class constructor in the subclass constructor, compiler insert super(), then has probelm locating no-argument parent class constructor ... so it complains.
it is not relevant to what refernce type (parent or child) being assigned to the subclass object.
help this help !


not so smart guy still curious to learn new stuff every now and then
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Sri Sri,
some correction for my 2nd comment :
In the subclass constructor, compiler will insert a super() if no "super" or "this" being the first statement inside subclass construtor.

Originally posted by chi Lin:
Sri Sri,
First of all, constructor is NOT inherited from the parent class. (you need define them OR compiler provide a no-argument constructor if not even one given in your code).
Secondary, creating a object require all upper hierarchy class being inialized/constructed.
<- which means in the child constructor, compiler will implicitly insert a super() at the first line if you does not explicity call the parent
constructor.

Combine above two rules, as you are creating a child object and you does not provide any code for parent class constructor in the subclass constructor, compiler insert super(), then has probelm locating no-argument parent class constructor ... so it complains.
it is not relevant to what refernce type (parent or child) being assigned to the subclass object.
help this help !
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Sri,
Thank you for using my exam.
As Basanti and Chi pointed out the compiler error is due to the fact that the compiler does not generate a default constructor for class T. However, the compiler does insert the super class constructor invocation statement, super(), as the first implicit statement of the constructor for class U. Remember, the super class is always constructed before the subclass. If the subclass constructor does not have as the first explicit statement the super class constructor invocation statement, super(), or an alternate constructor invocation statement, this(), then the compiler will insert the superclass constructor invocation statement, super().


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Thanks a lot
Dan and Chi
Best Wishes,
Sri
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheriting constructors