• 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

Why constructors are not inherited by child class

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic