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<
new. It cannot be called as a method.So inheritance is practically not possible as such. However in a construct one might call other constructors.
this(...);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.