Spaces:
Running
Running
@Override above the method
Browse files
Week 5: Class hierarchies/12A. Object-class methods - toString()
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Since all Java classes directly or indirectly inherit the Object class, all classes also inherit methods in the 'Object' class.
|
| 2 |
+
The METHODS of the Object class are special in that they are USU INTENDED TO BE OVERWRITTEN in
|
| 3 |
+
the inheriting classes - the basic functionality defined in the Object class is almost never sufficient.
|
| 4 |
+
|
| 5 |
+
In this section, let's look at a few methods found in the Object class and their sensible reimplementation.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
========================================================================
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Method toString
|
| 15 |
+
The toString() method, inherited from the Object class, is intended for situations where you want to
|
| 16 |
+
represent the CONTENTS of a class as a STRING.
|
| 17 |
+
For example, the print and println print methods can automatically call the toString() method of an object when the object is printed.
|
| 18 |
+
|
| 19 |
+
The default implementation of the method prints the CLASS NAME and the HASH CODE of the CLASS, separated by an @ sign. For example:
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Person {
|
| 24 |
+
protected String name;
|
| 25 |
+
protected String email;
|
| 26 |
+
|
| 27 |
+
public Person(String name, String email) {
|
| 28 |
+
this.name = name;
|
| 29 |
+
this.email = email;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
public String getName() {
|
| 33 |
+
return name;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
public void setName(String name) {
|
| 37 |
+
this.nimi = name;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
public String getEmail() {
|
| 41 |
+
return email;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
public void setEmail(String email) {
|
| 45 |
+
this.email = email;
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
public class PersonTest {
|
| 50 |
+
public static void main(String[] args) {
|
| 51 |
+
Person p = new Person("Pat Person", "[email protected]");
|
| 52 |
+
System.out.println(p);
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
Program outputs (for example)
|
| 57 |
+
Person@372f7a8d
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
========================================================================
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
Let's define the toString method for the Student class
|
| 67 |
+
so that it returns the data content in a more sensible format.
|
| 68 |
+
The method now also uses the @Override annotation.
|
| 69 |
+
It does not actually affect the execution of the program, but it ***tells Java that the method is to be overwritten***.
|
| 70 |
+
So, for example, if there is a misspelling in the name (say, toStrnig or tostring), Java will catch it at compile time.
|
| 71 |
+
|
| 72 |
+
class Person {
|
| 73 |
+
protected String name;
|
| 74 |
+
protected String email;
|
| 75 |
+
|
| 76 |
+
public Person(String name, String email) {
|
| 77 |
+
this.name = name;
|
| 78 |
+
this.email = email;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
public String getName() {
|
| 82 |
+
return name;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
public void setName(String name) {
|
| 86 |
+
this.name = name;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
public String getEmail() {
|
| 90 |
+
return email;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
public void setEmail(String email) {
|
| 94 |
+
this.email = email;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
@Override
|
| 98 |
+
public String toString() {
|
| 99 |
+
return this.name + ", email: " + this.email;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
Now when the Person object is printed, Java automatically calls the toString method.
|
| 105 |
+
Note that the toString method can of course also be called by itself,
|
| 106 |
+
for example when you want to store the string it returns in a variable.
|
| 107 |
+
|
| 108 |
+
Also, PRINTING A LIST AUTOmatically CALLS the toString method on all elements of the list.
|
| 109 |
+
|
| 110 |
+
public static void main(String[] args) {
|
| 111 |
+
Person p = new Person("Pat Person", "[email protected]");
|
| 112 |
+
// 1
|
| 113 |
+
// print the object
|
| 114 |
+
System.out.println(p);
|
| 115 |
+
|
| 116 |
+
Person p2 = new Person("Paula Personnel", "[email protected]");
|
| 117 |
+
Person p3 = new Person("Pete Personification", "[email protected]");
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
// 2
|
| 122 |
+
// print the object toString
|
| 123 |
+
String pat = "Here's person: " + p.toString();
|
| 124 |
+
System.out.println(pat);
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
// 3
|
| 128 |
+
//print the list of objects
|
| 129 |
+
ArrayList<Person> persons = new ArrayList<>();
|
| 130 |
+
persons.add(p);
|
| 131 |
+
persons.add(p2);
|
| 132 |
+
persons.add(p3);
|
| 133 |
+
System.out.println(persons);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
Program outputs:
|
| 137 |
+
Pat Person, email: [email protected]
|
| 138 |
+
Here's person: Pat Person, email: [email protected]
|
| 139 |
+
[Pat Person, email: [email protected],
|
| 140 |
+
Paula Personnel, email: [email protected],
|
| 141 |
+
Pete Personification, email: [email protected]]
|
| 142 |
+
|
| 143 |
+
|