• 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

Are constructors inherited in sub class?

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I read that constructors are not inherited while extending is at work. Why is it so? Another thing is all classes is the same package know about each others constructors by default. (unless access is restricted) So if the sub class can create objects of super class which is in the same package, what is the point of not allowing constructors not to be inherited.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the JLS:, "Constructors, static initializers, and instance initializers are not members and therefore are not inherited."

You should not confuse access with inheritance. Just because you have access to something else (that is you can use it) doesn't mean you inherit that thing. You should consider them two unrelated concepts, with rules on how one applies to the other: you can only inherit things you have access to. That does not imply you inherit everything you have access to.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Another thing is all classes is the same package know about each others constructors by default. (unless access is restricted) So if the sub class can create objects of super class which is in the same package, what is the point of not allowing constructors not to be inherited.


That's lazy thinking. You should always explicity define the visibility of everything you create (even if it's to leave it package-private); and if you're in any doubt, make it private.

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:According to the JLS:, "Constructors, static initializers, and instance initializers are not members and therefore are not inherited."

You should not confuse access with inheritance. Just because you have access to something else (that is you can use it) doesn't mean you inherit that thing. You should consider them two unrelated concepts, with rules on how one applies to the other: you can only inherit things you have access to. That does not imply you inherit everything you have access to.



But why is the language designed in this manner? Why are sub classes not allowed to inherit constructors?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:But why is the language designed in this manner? Why are sub classes not allowed to inherit constructors?

Why is usually not a question with an answer. It comes down to a decision made by people who are not here, and whose answer has little value in the present (unless you want to design a language and understand the decision making others have made). The language is the way it is, we just have to learn the rules and work with them.

So here is a question: why do you think Constructors should be inherited?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:So here is a question: why do you think Constructors should be inherited?


And furthermore: what would it mean? Constructors are not methods, and their use is strictly regulated.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic