Answer to Question #59562 in Java | JSP | JSF for Peter

Question #59562
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());
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS