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

Are constructors inherited in sub class?

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7811
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Mar 17, 2011
Posts: 7811
    
  21

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are constructors inherited in sub class?