• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inheriting constructors

 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !

 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
Dan and Chi
Best Wishes,
Sri
 
reply
    Bookmark Topic Watch Topic
  • New Topic