KaiquanMah commited on
Commit
5ecac4f
·
verified ·
1 Parent(s): f5d3ba7

FIND LATEST IMPLEMENTED METHOD vs instanceof vs getClass [OBJECT TYPE]

Browse files
Week 6: Methods of OO Programming/12A. Dynamic Binding+++ ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Let's also examine a situation where a METHOD is OVERRIDDEN in a subclass.
2
+ In the class "Lorry", the method getCapacity has been IMPLEMENTED AGAIN:
3
+
4
+ class Truck {
5
+ private int capacity;
6
+
7
+ public Truck(int capacity) {
8
+ this.capacity = capacity;
9
+ }
10
+
11
+ public int getCapacity() {
12
+ return this.capacity;
13
+ }
14
+ }
15
+
16
+
17
+ class Lorry extends Truck {
18
+ private int trailerCapacity;
19
+
20
+ public Lorry(int capacity, int trailerCapacity) {
21
+ super(capacity);
22
+ this.trailerCapacity = trailerCapacity;
23
+ }
24
+
25
+ // HERE
26
+ @Override
27
+ public int getCapacity() {
28
+ return super.getCapacity() + trailerCapacity;
29
+ }
30
+ }
31
+
32
+
33
+
34
+
35
+
36
+
37
+ Let's create a new object of type "Lorry", and save the reference to a "Truck"-type variable.
38
+
39
+ The type of the variable defines the operations that can be used:
40
+ thus, we can only call methods defined in the "Truck" class.
41
+ So let's call the "getCapacity" method, which is defined in the "Truck" class and overridden in the "Lorry" class.
42
+
43
+ public class LorryTest {
44
+ public static void main(String[] args) {
45
+
46
+ //INITIALISE 'Lorry' object as a 'Truck' / superclass
47
+ Truck vehicle = new Lorry(10, 20);
48
+ System.out.println("Capacity: " + vehicle.getCapacity());
49
+ }
50
+ }
51
+
52
+ The program outputs
53
+ Capacity: 30
54
+
55
+
56
+
57
+
58
+ From this example, we notice that the method call is directed to the implementation given in the Lorry class, even though the type of the variable is Truck.
59
+ As a general rule,
60
+ - The VARIABLE TYPE DETERMINES the AVAILABLE OPERATIONS / METHODS
61
+ - The OBJECT TYPE determines the IMPLEMENTATIONS used by the METHODS
62
+
63
+ This is called dynamic binding: Java always looks for the LATEST IMPLEMENTATION of each METHOD.
64
+ https://docs [dot] google [dot] com/presentation/d/e/2PACX-1vTpe_7VNECaErEVyCbdD2Ud_CcEaS7sDoG7YD5ic--v34mX0QCzEZRQ-q-ENqkjTI6wiGT-Kh0rHNPl/embed?start=false&loop=false&slide=id.p
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+ ==========================================
77
+
78
+
79
+ Note that dynamic binding works in the same way also with internal calls of an object.
80
+
81
+ Let's look at a situation where the class 'DetectiveNovel' inherits from the class 'Book'.
82
+ In the "DetectiveNovel" class, the 'getTitle' method is overridden, but not the toString method.
83
+
84
+ class Book{
85
+ public String getTitle(){
86
+ return "book - ";
87
+ }
88
+
89
+ public String getPublisher(){
90
+ return "publisher";
91
+ }
92
+
93
+ public String toString(){
94
+ return getTitle() + getPublisher();
95
+ }
96
+ }
97
+
98
+
99
+ class DetectiveNovel extends Book {
100
+
101
+ @Override
102
+ public String getTitle() {
103
+ return "detective novel - ";
104
+ }
105
+ }
106
+
107
+
108
+
109
+
110
+ When the "toString" method is called for a "DetectiveNovel"-type object,
111
+ it always uses the "DetectiveNovel" class implementation for the "getTitle" method:
112
+
113
+ public class DetectiveNovelTest {
114
+ public static void main(String[] args) {
115
+ Book book = new DetectiveNovel();
116
+ System.out.println(book.toString());
117
+ }
118
+ }
119
+
120
+
121
+ // 'Book' toString()
122
+ // IMPLEMENTs getTitle() + getPublisher()
123
+ // ie 'DetectiveNovel'/OVERRIDDEN getTitle() + 'Book'/ORIGINAL getPublisher()
124
+ The program outputs:
125
+ detective novel - publisher
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+ ==========================================
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+ Determining the Type of an Object
149
+ As the TYPE of an OBJECT DETERMINES the outcome of a METHOD CALL in some situations,
150
+ we need to be able to determine the type.
151
+ This is done, for instance, with the operator 'instanceof'.
152
+ The operator returns the value true, if the given object is of the given class type, i.e.
153
+
154
+
155
+ A instanceof B
156
+ obj1 instanceof Class1
157
+ variable1 instanceof Class1
158
+ returns the value true if A's type is B.
159
+
160
+
161
+
162
+
163
+ An example clarifies the situation:
164
+ public static void main(String[] args) {
165
+ Person ollie = new Student("Oliver", "12345", 123);
166
+ System.out.println(ollie instanceof Student);
167
+ System.out.println(ollie instanceof Person);
168
+ System.out.println(ollie instanceof Object);
169
+ System.out.println(ollie instanceof Teacher);
170
+ }
171
+ The Program outputs:
172
+ true
173
+ true
174
+ true
175
+ false
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+ So, a "Student" type is both a "Student", "Person" and "Object".
185
+ In fact, the statement
186
+
187
+ A instanceof Object
188
+ is true for all Java objects, as "Object" is the superclass of all classes.
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+ If there is a need TO DETERMINE the "ACTUAL" TYPE of an OBJECT and the type of the superclass is not sufficient,
199
+ we can use the method "getClass":
200
+
201
+ public static void main(String[] args) {
202
+ Person oliver = new Student("Oliver", "12345", 123);
203
+
204
+ System.out.println("Oliver is a Person: " + (oliver instanceof Person));
205
+ System.out.println("Oliveris a Person: " + (oliver.getClass() == Person.class));
206
+
207
+ System.out.println("Oliver is a Student: " + (oliver instanceof Student));
208
+ System.out.println("Oliver is a Student: " + (oliver.getClass() == Student.class));
209
+ }
210
+
211
+ Ohjelma tulostaa:
212
+ Oliver is a Person: true // 'Student' object type is a subclass of 'Person'
213
+ Oliver is a Person: false // 'Student' object type was not initialised as a 'Person' class
214
+ Oliver is a Student: true // 'Student' object type
215
+ Oliver is a Student: true // 'Student' object type was initialised as a 'Student' class
216
+
217
+
218
+
219
+ ====================
220
+
221
+
222
+
223
+
224
+ A typical example is the "equals" method,
225
+ where it is determined whether an object is of the same type as the object being compared
226
+ - see an example by generating the "equals" method in Eclipse!
227
+
228
+ Let's consider an example of a method that receives a list of "Person"-class type objects
229
+ and prints information about each object,
230
+ whether it is a "Person", "Student" or "Teacher" object:
231
+
232
+ // VARIABLE TYPE = 'Person'
233
+ // OBJECT TYPE => depends on HOW OBJECTS WERE INITIALISED
234
+ public static void whatType(ArrayList<Person> people) {
235
+ for (Person person : people) {
236
+
237
+ if (person.getClass() == Person.class) {
238
+ System.out.println("This is a person!");
239
+ }
240
+ else if (person.getClass() == Student.class) {
241
+ System.out.println("This is a student!");
242
+ }
243
+ else if (person.getClass() == Teacher.class) {
244
+ System.out.println("This is a teacher!");
245
+ }
246
+ }
247
+ }
248
+
249
+
250
+
251
+
252
+