63 166
Assignments Done
99,3%
Successfully Done
In July 2018

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);
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions