class Base:
public class Base {
private String name = "base";
public Base() {
tellName();
printName(name);
}
public void tellName() {
System.out.println("Base tell name:" + name);
}
public void printName(String name) {
System.out.println("Base print name:" + name);
}
}
class Derived:
public class Derived extends Base {
private String name = "Derived";
public Derived() {
tellName();
printName(name);
}
public void tellName() {
System.out.println("Derived tell name:" + name);
}
public void printName(String name) {
System.out.println("Derived print name:" + name);
}
public static void main(String[] args) {
Derived derived = new Derived();
}
}
Result:
Derived tell name:null
Derived print name:base
Derived tell name:Derived
Derived print name:Derived
Recently, I was asked this question. I thought about it, but didn't have exact answer of why this program section has such execute result. Hope someone can help me analyze such problem. Thanks a lot! ^-^
I just want to know the execution process of constructor in derived condition.