changing `.toString()` in subclasses without overwriting everything

// in class Person (personID, name, age)
@Override
public String toString(){
return String.format("Person: %04d: [name:%s, age:%d]", this.personID, this.name, this.age) ;
}
// in class Person (personID, name, age)
@Override
public String toString(){
return String.format("Person: %04d: [name:%s, age:%d]", this.personID, this.name, this.age) ;
}
// in class Student (super(personID, name, age), degree)
@Override
public String toString() {
// current state, since i dont know how to do it different
return "Student{} " + super.toString();
}
// in class Student (super(personID, name, age), degree)
@Override
public String toString() {
// current state, since i dont know how to do it different
return "Student{} " + super.toString();
}
public static void main(String[] args) {

Person freddi = new Person("Freddi", 30);
Person richard = new Person("Richard", 30);
Person andre = new Student("André", 21);

System.out.println(freddi.toString());
System.out.println(richard.toString());
System.out.println(andre.toString());
}
public static void main(String[] args) {

Person freddi = new Person("Freddi", 30);
Person richard = new Person("Richard", 30);
Person andre = new Student("André", 21);

System.out.println(freddi.toString());
System.out.println(richard.toString());
System.out.println(andre.toString());
}
thats the output i get:
Person: 0001: [name:Freddi, age:30]
Person: 0002: [name:Richard, age:30]
Student{} Person: 0003: [name:André, age:21]
Person: 0004: [name:Freddi, age:30]
Person: 0005: [name:Richard, age:30]
Student{} Person: 0006: [name:André, age:21]
Person: 0001: [name:Freddi, age:30]
Person: 0002: [name:Richard, age:30]
Student{} Person: 0003: [name:André, age:21]
Person: 0004: [name:Freddi, age:30]
Person: 0005: [name:Richard, age:30]
Student{} Person: 0006: [name:André, age:21]
and the output i want to get is:
// I know it makes no sense having one ID for Person & Student.
// its just for practise
Person: 0001: [name:Freddi, age:30]
Person: 0002: [name:Richard, age:30]
Student: 0003: [name:André, age:21]
Person: 0004: [name:Freddi, age:30]
Person: 0005: [name:Richard, age:30]
Student: 0006: [name:André, age:21]
// I know it makes no sense having one ID for Person & Student.
// its just for practise
Person: 0001: [name:Freddi, age:30]
Person: 0002: [name:Richard, age:30]
Student: 0003: [name:André, age:21]
Person: 0004: [name:Freddi, age:30]
Person: 0005: [name:Richard, age:30]
Student: 0006: [name:André, age:21]
My Question Is there a way to just change a part of the .toString() method without @Overrideing it entirely. one bothersome solution i thought of is creating a formater taking a String (String type = "Person") created by the constructor. but still itd be botherring.
5 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @Darknesslion5|Christian! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3w ago
you can use getClass().getName()/getClass().getSimpleName()
// in class Person (personID, name, age)
@Override
public String toString(){
return String.format("%s: %04d: [name:%s, age:%d]", this.getClass().getSimpleName(), this.personID, this.name, this.age) ;
}
// in class Person (personID, name, age)
@Override
public String toString(){
return String.format("%s: %04d: [name:%s, age:%d]", this.getClass().getSimpleName(), this.personID, this.name, this.age) ;
}
Darknesslion5|Christian
thanks, i currently did it with String[] type = getClass().toString().split("[.]"); and type[1] but getClass().getSimpleName() is definetly better xd
JavaBot
JavaBot3w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot3w ago
Post Closed
This post has been closed by <@245536392308981760>.

Did you find this page helpful?