I have 3 classes (abstract) Number
, One
(extends Number
), Next
(extends Number
).
I want to assign value 1 to object of type One so that calling:
Number one = new One();
Number four = new Next(new Next(new Next(one)));
System.out.println(one.toString());
System.out.println(four.toString()):`
prints:
1
n(n(n(1)))
If I get what you're asking, override the Object.toString
method.
In One
:
@Override public String toString() {
return "1";
}
In Next
:
@Override public String toString() {
return "n("+next+")";
}
Assuming you Next
field for the next node is called next
.
You may also wish to make toString
abstract in Number
:
@Override public abstract String toString();