I created an object called 'saving's and I want to use it to combine & display information I have in 3 different toString() methods that are also in 3 different classes. How do I do this? I have tried putting super.toString(); at the end of each toString method and also extended the classes, but it will only print out one of the toString methods when I either do savings.toString(); or System.out.println(savings);
1
Expert's answer
2016-06-08T08:35:02-0400
It is necessary to use the notation @Override. The idea is realized in the code below:
public class Savings { @Override public String toString() { W w = new W(); Q q = new Q(); R r = new R(); return q.toString() + w.toString() + r.toString(); } }
public class Q { @Override public String toString() { return "Q"; } }
public class W { @Override public String toString() { return "W"; } }
public class R { @Override public String toString() { return "R"; } }
public class Application { public static void main(String[] args) { Savings s = new Savings(); System.out.println(s.toString()); } }
Comments
Leave a comment