TurkuBasicOOPinJava / Week 5: Class hierarchies /12A. Object-class methods - toString()
KaiquanMah's picture
@Override above the method
e4fa2d9 verified
raw
history blame
3.88 kB
Since all Java classes directly or indirectly inherit the Object class, all classes also inherit methods in the 'Object' class.
The METHODS of the Object class are special in that they are USU INTENDED TO BE OVERWRITTEN in
the inheriting classes - the basic functionality defined in the Object class is almost never sufficient.
In this section, let's look at a few methods found in the Object class and their sensible reimplementation.
========================================================================
Method toString
The toString() method, inherited from the Object class, is intended for situations where you want to
represent the CONTENTS of a class as a STRING.
For example, the print and println print methods can automatically call the toString() method of an object when the object is printed.
The default implementation of the method prints the CLASS NAME and the HASH CODE of the CLASS, separated by an @ sign. For example:
class Person {
protected String name;
protected String email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.nimi = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public class PersonTest {
public static void main(String[] args) {
Person p = new Person("Pat Person", "pat@example.com");
System.out.println(p);
}
}
Program outputs (for example)
Person@372f7a8d
========================================================================
Let's define the toString method for the Student class
so that it returns the data content in a more sensible format.
The method now also uses the @Override annotation.
It does not actually affect the execution of the program, but it ***tells Java that the method is to be overwritten***.
So, for example, if there is a misspelling in the name (say, toStrnig or tostring), Java will catch it at compile time.
class Person {
protected String name;
protected String email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return this.name + ", email: " + this.email;
}
}
Now when the Person object is printed, Java automatically calls the toString method.
Note that the toString method can of course also be called by itself,
for example when you want to store the string it returns in a variable.
Also, PRINTING A LIST AUTOmatically CALLS the toString method on all elements of the list.
public static void main(String[] args) {
Person p = new Person("Pat Person", "pat@example.com");
// 1
// print the object
System.out.println(p);
Person p2 = new Person("Paula Personnel", "paula@example.com");
Person p3 = new Person("Pete Personification", "pete@example.com");
// 2
// print the object toString
String pat = "Here's person: " + p.toString();
System.out.println(pat);
// 3
//print the list of objects
ArrayList<Person> persons = new ArrayList<>();
persons.add(p);
persons.add(p2);
persons.add(p3);
System.out.println(persons);
}
Program outputs:
Pat Person, email: pat@example.com
Here's person: Pat Person, email: pat@example.com
[Pat Person, email: pat@example.com,
Paula Personnel, email: paula@example.com,
Pete Personification, email: pete@example.com]