Java inheritance - constructors
While studying for my finals, I came across the following statement in the book from which I am currently studying. Considering the following code :
class A {
public A(int x) { }
}
class B extends A {
public B(int x ) { }
}
is it mandatory to call the constructor of class A in the constructor of class B(super(x)). The book states that it's not mandatory, because they have the exact number and type of parameters. But when I tr开发者_StackOverflow社区y this in a java compiler, the following error gets thrown :
constructor A in class A cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length
The compiler automatically inserts super()
in the beginning.
However, even constructors arguments, super()
(without arguments) is added which invokes the default constructor of the superclass. And you don't have one, hence the error.
You have to specify super(x)
(to invoke A(x)
), or define a no-argument constructor.
By the way, Eclipse compiler gives a way better error message:
Implicit super constructor A() is undefined. Must explicitly invoke another constructor
It looks like the compiler tries to create a call to the superclasses default constructor with super()
, which isn't avaliable:
required: int
found: no arguments
But back to your book: I've never heard of a rule that you can skip the super
statement in a constructor if the actual constructor has the exact same parameter list as a constructor in the direct superclass. Only a call to the superclass's default constructor is added implicitly (super()
) but that requires that the superclass has a default constructor.
In contrast to what's written in your book (or in contrast to your understanding of the written text), here's a sentence from the language spec:
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a super class constructor invocation “super();”, an invocation of the constructor of its direct superclass that takes no arguments.
If you have a the base class having a default constructor (no-arg constructor), when you extend B
, you don't need to explicitly call super()
because it is called any way.
But when you have a constructor with arguments, when making the contructor with parameters in B
, you need to pass in super()
a parameter for A
example :
class A {
public A(int x) { }
}
class B extends A {
public B(int x )
{
super(x); // need to specify the parameter for class A
//...
}
}
This happens when you dont have a default constructor and you are creating an instance with default one. Because If you have any Parameterized constructor then compiler will not insert the default one for you, instead you have to define.
It is neccessary to call constructor of super class in case of Java. Therefore, whenever you generate constructor of sub class, super class' constructor is self created by IDE.
It is because whenever base class constructor demands arguments, compiler thinks they are to be filled by base class constructor. In case of default constructor, it is OK. No need to call super() in sub class.
精彩评论