abstract class Animal {
private String name;
public Animal() {
this.name = "someName";
}
public String getName(){
return this.name;
}
}
class Dog extends Animal {
private String breed;
public Dog() {
// super(); <----- not calling it
this.breed = "someBreed";
}
}
public class TryOO {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println(dog.getName());
}
}
输出
someName
好像已经设置了变量但没有从Dog调用super()吗?
如果抽象类和对其扩展的类在其构造函数中均不包含任何参数,则似乎无需调用super()。
这是真的吗?