+ 3
Do subclasses inherit the constructors of super class?
2 odpowiedzi
+ 3
Taken as is from the course material on inheritance, 3rd slide :
Constructors are not member methods, and so are not inherited by subclasses.
However, the constructor of the superclass is called when the subclass is instantiated.
:)
+ 2
If you want to call the constructor of the superclass explicitly, you have to do it in the subclasses constructor with super(); ! It also has to be the first thing you do in the subclasses constructor:
class A {
private string name;
public A(string name) {
this.name = name;
}
}
class B extends A {
public B(string name) {
super(name);
//do specific b constructor stuff here
}
}
class Test {
public static void main(String[] args) {
B b = new B("i am an object of b");