4

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Raghu Kariganur
  • 41
  • 1
  • 1
  • 4

4 Answers4

8

Unlike fields, methods, and nested classes ,Constructors are not class members.

From docs of Inheritance

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 ??

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
5

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.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • 1
    What you describes also happens when a library adds new methods. In your explanation there is no reason not to inherit constructors. (Sorry for being blind) – Orri Apr 12 '17 at 15:26
  • 1
    @Orri It's not the same. It's a perfectly fine design choice for a certain subtype to *require* more information than its supertype before it can be constructed for the first time (because the type is known exactly at construction). But a method requiring more information to operate on a subtype than it needs to operate on a supertype is a design flaw (violation of LSP). – Theodoros Chatzigiannakis Apr 12 '17 at 21:42
1

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.)

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
0

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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136