• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why constructors are not inherited by child class

 
Fran Varin
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seb Mathe
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic