Why are constructors not inherited in java?
Solution 1
In simple words, a constructor cannot be inherited, since in subclasses it has a different name (the name of the subclass).
class A {
A();
}
class B extends A{
B();
}
You can do only:
B b = new B(); // and not new A()
Methods, instead, are inherited with "the same name" and can be used.
As for the reason: It would not have much sense to inherit a constructor, since constructor of class A means creating an object of type A, and constructor of class B means creating an object of class B.
You can still use constructors from A inside B's implementation though:
class B extends A{
B() { super(); }
}
Solution 2
What you are talking about is Java language level. If constructors were inherited, that would make impossible to make class private. As we know method visibility can't be downgraded. Object
class has a no argument constructor and every class extends Object
, so in case of constructor inheritance every class would have a no argument constructor. That breaks OO principles.
Things are different on bytecode level. When object is created, two operators are called:
- new - allocates memory for object
- invokespecial - calls constructor on newly allocated piece of memory
We can modify bytecode so that memory is allocated for Child class and constructor is called from Parent class. In this case we can say that constructors are inherited. One notice if we don't turn off byte code verification, JVM will throw an exception while loading class. We can do this by adding -noverify
argument.
Conclusion:
- Constructors are not inherited on language level due to OO principles
- Constructors are inherited on bytecode level
Solution 3
Reason mentioned in 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.
You can refer docs of Providing Constructors for Your Classes
Solution 4
Constructors are not members of classes and only members are inherited. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses.
Solution 5
- A constructor may only be called with
new
. It cannot be called as a method. - The constructor name is identical to the class name.
So inheritance is practically not possible as such. However in a construct one might call other constructors.
- In the same class using
this(...)
; - Of the extended class using
super(...)
;
Example
class A {
A() { } // Constructor
A(int a) { } // Constructor
A(boolean c) { } // Constructor
}
class B extends A {
B() {
this(3, 7);
}
B(int a) {
super();
}
B(String b) {
super(7);
}
B(int a, int c) { // Calls super() implicitly
}
}
A a = new B(8):
There unfortunately is no possibility to use A's constructor for a boolean:
B b = new B(true): // ERROR
The language designes could have implemented such a thing as:
Generate for every public constructor in the base class, a constructor with the same signature if such a constructor is not defined already. Call super
with the same parameters. Call this()
if there is a default constructor.
That seems a bit bloating the code. And is not simply a pointer in a virtual method table, by which method inheritance/overriding works.
Related videos on Youtube
Mayank Tiwari
I am Mayank Tiwari Pursuing M. Tech from Indian Institute of Information Technology Design And Manufacturing Jabapur.
Updated on June 02, 2021Comments
-
Mayank Tiwari almost 3 years
I am a beginner in java programming language, recently I have studied that constructors can not be inherited in java, Can anyone please explain why?
I have already read this link of C++
-
keyser over 10 yearsYou do have
super()
though.
-
-
Mayank Tiwari over 10 yearssir I have also read this, constructors are special member functions of a class whose name is same as that of class, but they do not have any return type. Is it TRUE?
-
Suresh Atta over 10 years@user2320537 If you mean
data type
meansreturn type
, yes they don't have anyreturn type
. -
Paul Brewczynski over 10 yearsDefine "inheriting". Doesn't inheriting means that you could "USE" it. So you basically inherit constructors ?
-
Lake about 10 yearsAs far as my explanation is concerned, "inheriting" means that you can use method X inherited by class B from class A PUBLICLY (i.e. outside class B's constructor scope), for example B b = new B(); b.X(); You cannot do B b = new A(); so you could not publicly use "inherited constructors" even if you'd declare them public, even inside class B. If you think being able to call constructor A inside B's constructor means inheritance, then inheritance be it ^^
-
Damn Vegetables over 8 yearsNow I know "why", but I still wish there were some syntax sugar to eliminate those constructors that just call parent's constructors with the same arguments.
-
user1122069 about 8 years"a constructor cannot be inherited, since in subclasses it has a different name" - what language a design choice! ROTFL. We wanted to make classes hard to inherit and hard to refractor, and constructors hard to write. Either that or they were just so in love with this idea of self-named constructors that they refused or never thought to rename them. We did it this way so it must continue forever as tradition.
-
Lake about 8 yearsI think it's actually quite reasonable of a choice to let the constructors have their defining class' name, defining a constructor that just calls super with the same arguments doesn't look like hard work for me.. there might be better way to design the language though.
-
Aetherus almost 8 yearsIt is a hard work for me because my superclass has a parameterized constructor which constructs the object through reflection. That means subclasses don't need to do anything even if they have different member fields that should be initialized. Despite this fact, the subclasses have to implement their own constructors only to call
super(...)
. If you have dozens of such subclasses, this will be an overhead. -
Amal lal T L almost 6 yearsThe parent class constructor is by default called from the child class constructor.
-
Lake almost 6 yearsYou're right, I failed to mention that. Only if it doesn't have any arguments.
-
Gzorg about 5 yearsI find that in the case of custom exceptions, one usually want to have all the same constructors as the base class, so in these circumstances, it would make sense for java to generate default constructors which all call super by default, when no constructors are provided.
-
Deduplicator over 2 yearsNot being able to initialize a reference to Derived with a reference to Base has exactly nothing to do with inheriting constructors. How constructors are named has the same non-relationship to their inheritability. No wonder this won the prize.
-
Kannan Ramamoorthy about 2 yearsWhen you say the constructors are inherited at bytecode level, I assume that is not the default and that happens only if we explicitly modify
invokespecial
to call parent constructor. Please confirm.