File size: 3,883 Bytes
e4fa2d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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]