I think I’m misunderstanding something, and if so, I need help..
Let’s say I have the following two classes:
public abstract class Abstraction() {
protected int number = 0;
public void printNumber() {
System.out.println(this.number);
System.out.println(getNumber());
}
public int getNumber() {
return this.number();
}
}
public class Reality extends Abstraction {
int number = 1;
public Reality() {
this.printNumber();
}
}
// some other class
new Reality();
What should the output be? I’m getting 0, 0 (the code here is a bit more complicated but still the same issue).
How can I get the output to be 1,1?
The
numberin theRealityclass doesn’t overwrite thenumberin theAbstractionclass. Therefore, theAbstractionclass still sees his ownnumbervalue as0.A solution would be:
Because your
numberinAbstractionis protected, you can access it in yourRealityclass.Another example is a parameter in a method:
Your
private int numberfield won’t be set, instead, the parameternumberwill be set as2.Finally, a word on the
thisandsuperkeywords, see an edit of your classes: