So i have come across this task in my studies and i can’t figure out the right track of the program and can’t explain myself this output. Here is the code:
public class Main {
public static void main(String[] args) {
SuperClass o1 = new SubClass(5, 2);
System.out.print(o1);
}
}
class SuperClass {
private int value;
public SuperClass(int value) {
setValue(value);
}
public void setValue(int value) {
System.out.println("Entered setValue2");
this.value = value;
}
@Override
public String toString() {
return "value=" + value;
}
}
class SubClass extends SuperClass{
private final int factor;
public SubClass(int value, int factor) {
super(value);
this.factor = factor;
}
@Override
public void setValue(int value) {
System.out.println("Entered setValue1");
super.setValue(factor * value);
}
@Override
public String toString() {
return super.toString() + ", factor=" + factor;
}
}
So i tried to track the program without success and was not able to figure out why this output happens:
Entered setValue1
Entered setValue2
value=0, factor=2
New contributor
Leon Kiss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.