So... I have a parent class which overrides default toString() method and another class which inherits (extends) the first class.
Is there a way to override toString() method of the second class so it just to append text next to the output of toString() method of its parental class?
This is the code of the parental class:@Override
public String toString(){
return String.format("First Name: %s%nLast Name: %s", getFirstName(), getLastName());
}
I would like just to append Facility number: %s
at the end of the string.
Asked
Active
Viewed 894 times
-2

Savina Dimitrova
- 155
- 3
- 9
-
1Yes. Post your code. – Elliott Frisch Sep 02 '17 at 21:17
-
4Something like `public String toString(){return super.toString() + "additonal part";}`? – Pshemo Sep 02 '17 at 21:17
-
Thank you. It works :) I guessed that it will be something similar to the constructor invocation with "super" but I didn't know how to write it. – Savina Dimitrova Sep 02 '17 at 21:27
1 Answers
1
You can get use super before toString() method. It let you use method from base class. The word this give you method from class which is extended by base class.

sszledak
- 11
- 2