Reputation: 53
I am creating a program with many subclasses (A through G) and am trying to use the toString method to create a printable representation of an object reference from the subclass G. Class G inherits from class F. I'm not too familiar with toString yet and can't figure out how to do something like this. Thanks for any help!
Upvotes: 1
Views: 4415
Reputation: 72
Override the toString() method in the class G where you will have the access to the members of G and its parent classes till its root in the hierarchy. Prepare a string out of all the instance variables by concatenating with meaningful prefixes. example: "Age:"+ this.age+"Salary:"+this.salary; Hope this is helpful.
Upvotes: 0
Reputation: 1032
You have to override the toString()
method from the superclass:
@Override
public String toString() {
return getClass().getCanonicalName();
}
The above will simply return the canonical name of the class
Upvotes: 0
Reputation: 223043
@Override
public String toString() {
return /* fill this in */;
}
Upvotes: 2