class Test {
public static void main(String[] args) {
new TeamLead(1);
}
public static class TeamLead extends Programmer {
private int numTeamLead=2;
public TeamLead(int numTeamLead) {
super(numTeamLead);
this.numTeamLead = numTeamLead;
employ();
}
protected void employ() {
System.out.println(numTeamLead + " teamlead");
}
}
public static class Programmer {
private final int numProgrammer;
public Programmer(int numProgrammer) {
this.numProgrammer = numProgrammer;
employ();
}
protected void employ() {
System.out.println(numProgrammer + " programmer");
}
}
}
Why is the first output 0 teamlead
instead of 2 teamlead
?
I declared numTeamLead=2
before executing employ()
and yet the first time employ()
is executed, numTeamLead
defaults to 0
for some reason. I know that the super(numTeamLead)
cannot use the Programmer.employ()
because it is protected
, so it uses TeamLead.employ()
instead. But why then does it default numTeamLead
to 0
before using employ()
again, even though I already initialized numTeamLead
prior to executing any method?
30pct is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.