Reputation: 41
I have been learning constructors in Inheritance using Eclipse Juno.
When I press ctrl+O twice in the childClass, It shows inherited members. But I happen to see even the Constructor of super class in the inherited members
But it is said that constructors are not inherited...
Can someone please explain this behaviour?
Upvotes: 4
Views: 15707
Reputation: 122006
Unlike fields, methods, and nested classes ,Constructors are not class members.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
But why constructor removed from class member ??
Upvotes: 8
Reputation: 29223
Only members are inherited, and a constructor is not considered a member.
To understand why constructors are not inherited, consider that inheritance in OOP is part of the mechanism that allows an object to be treated as another, more general object. This requires that all data members and all methods are inherited.
What inheritance is not intended to do, is allow one object to be instantiated in the same manner as another, more general object.
This is because a constructor must initialize an object to a valid state and what's enough information to initialize valid state for a superclass object might not be enough information to initialize valid state for the subclass object!
To work around this, if constructors were inherited, when you extended a class from a library you'd have to manually opt out of the constructors you don't want inherited. This is cumbersome and error prone, as when a new version of that library comes out with more constructors in that base class, your own class is now subject to invalid initializations (through the leaked constructors), unless you release an update too. Or it could be that adding a constructor in your own superclass will "break" your own subclasses and you'd have to go to each subclass and opt out of the new ones. In other words, the validity of your code would be more tightly coupled to the base you've used.
On the other hand, "opting in", by defining your own constructors explicitly and chaining them with those of the base class makes more sense practically and is safer for the validity of your component. This opting in is done by chaining, the process of invoking a base class constructor at the beginning of another constructor.
Now Eclipse (which I don't use, so I'm basing this on what you're describing in your question) may probably be listing the constructors available for use in chaining because looking for them is a very common scenario (unless you're invoking a very simple or parameterless constructor). In other words, the constructors are listed in the inherited members for convenience but, as we've said, strictly speaking, they are not inherited.
Upvotes: 5
Reputation: 68715
Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
Source: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Eclipse method help using Ctrl+O shows what all methods that you can call from the current class. Hence parent constructors are also displayed in that as you can call it using super
.
Upvotes: 0
Reputation: 223133
Constructors are chained: each constructor you write must eventually invoke one of the superclass constructors. Example:
public class MyException extends RuntimeException {
public MyException(String message) {
super(message); // invokes RuntimeException(String) constructor
}
}
A super(...)
or this(...)
constructor invocation, if any, must appear as the first statement in your constructor. If neither of those is specified, super()
is implicitly assumed, which will chain up to the superclass's default constructor. (And if the superclass has no default constructor, then the compilation will fail.)
Upvotes: 1