aspose file tools*
The moose likes Beginning Java and the fly likes Why constructors are not inherited by child class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why constructors are not inherited by child class" Watch "Why constructors are not inherited by child class" New topic
Author

Why constructors are not inherited by child class

Fran Varin
Greenhorn

Joined: Aug 08, 2003
Posts: 22
If I have a parent class with a constructor that takes a parameter and I create a child class that extends the parent I cannot instantiate the child by using the parent's parameterized constructor. I must create a constructor with the same signature and call super().

While I can accept thsi behavior as fact, I cannot understand the rationale for such a policy within the language. Would someone care to expound on the philosophy on why this behavior is part of the language?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
My belief is that the fewer implicit things that happen in a programming
language the better. For example, I'd prefer there to be an explicit
"package" level access, rather than it being the so-called default level:

Anyhowdy, if you derive from a class, you must explicitly provide
constructors, rather than have them automatically generated:

I can think of several reasons for why this is good language design,
for instance, my preference for avoiding implicitly generation.

Another reason why is the following. Suppose the rule was that
if you provided *no* constructors, Java would try to generate
the obvious constructors based on the base class. That may be okay
today, but suppose tomorrow, a new constructor was aded to A, one
that you didn't want B to have, yet by the above rule, it would
sneak into your code.

If you find typing in the matching constructors in subclasses
tedious, realize that most IDEs will slap the code down for
you in a twinkle.


There is no emoticon for what I am feeling!
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
First of all, constructors are not members of classes, and only memebers are inherited.

Second, we can imagine cases for which we don't want subclasses to have the same constructors than the parent class.

Imagine an abstract class Vehicle with a constructor Vehicle(int wheels), and a subclass Bicycle.

By definition, a Bicycle has 2 wheels so we can imagine that Bicycle constructor will call super(2) and isn't it better in this case that Bicycle does not expose a constructor Bicycle(int wheels) ?


Regards,<br />Seb<br /> <br />SCJP 1.4
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
> First of all, constructors are not members of classes, and only memebers are inherited.

True, but the OP was looking for a rationale, and this just a technicality.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"Gabby Hayes"-

Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why constructors are not inherited by child class