• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are constructors inherited in sub class?

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic