25

In a subclass we can initialize data members using the subclass's constructor which internally calls the superclass's constructor super(). If a subclass can't inherit constructors from its superclass then how can the super() call initialize the superclass?

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
AmitSource
  • 251
  • 1
  • 3
  • 4

7 Answers7

43

A constructor from a subclass can call constructors from the superclass, but they're not inherited as such.

To be clear, that means if you have something like:

public class Super
{
    public Super(int x)
    {
    }
}

public class Sub extends Super
{
    public Sub()
    {
        super(5);
    }
}

then you can't write:

new Sub(10);

because there's no Sub(int) constructor.

It may be helpful to think of constructors as uninherited static methods with an implicit parameter of the object being initialized.

From the Java Language Spec, section 8.8:

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    Not very DRY there, Java! --A C++ wienie – Phlip Sep 27 '13 at 04:04
  • 2
    @Phlip: You only repeat yourself if you have the exact same requirements - at which point it's only a matter of repeating the declarations, explicitly invoking the appropriate superclass constructor. I see no reason why Java should *assume* that the parameters required to construct an instance of a subclass are the same as the parameters required to construct an instance of the superclass. I'm fine with this decision. – Jon Skeet Sep 27 '13 at 05:35
4

No a subclass cannot inherit the constructors of its superclass.

Constructors are special function members of a class in that they are not inherited by the subclass. Constructors are used to give a valid state for an object at creation.

One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

See also : Constructors are never inherited

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
2

super is a Java keyword for referring to the superclass, and super() is the way to call your superclass's constructor. The constructor is not inherited but you are still able to call it.

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
1

No..Not at all never...A Subclass Does not inherits its Superclass constructor..instead it call it can call its Superclass Constructor Using keyword "super()"...

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
0

First of all, constructors are not members of classes, and only members are inherited.

Second, we can imagine cases for which we don't want subclasses to have the same constructors than the parent class.

Imagine an abstract class Vehicle with a constructor Vehicle(int wheels), and a subclass Bicycle.

By definition, a Bicycle has 2 wheels so we can imagine that Bicycle constructor will call super(2) and isn't it better in this case that Bicycle does not expose a constructor Bicycle(int wheels) ?

Aasif Ali
  • 11
  • 3
0
public class Super
{
    public Super(int x)
    {
    }
}

public class Sub extends Super
{
    public Sub(int x)
    {
        super(x); // call constructor from Super class 
    }
}
Sub obj = new Sub(5);
John Conde
  • 217,595
  • 99
  • 455
  • 496
-1

Subclasses inherit all the members from their superclass, but a constructor is not consider to be a member, that's why constructors are not inherited by subclasses, but we can call superclass from subclass using super keyword.

Abra
  • 19,142
  • 7
  • 29
  • 41
MLone
  • 1
  • 1