KaiquanMah commited on
Commit
3b7dc42
·
verified ·
1 Parent(s): 9f2f0be

interface INTERFACE1 {dtype method1();}

Browse files
Week 6: Methods of OO Programming/05A. Interfaces+++ ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ It is common that there is a PROPERTY we would like to declare for MULTIPLE CLASSES.
2
+ For example, we could have several classes that have a price.
3
+ However, since a price can be attached to very different classes (for example a car, a banana, an airplane, and a house),
4
+ it would be difficult to come up with a common superclass.
5
+
6
+
7
+ For this, we have INTERFACES.
8
+ The idea of an interface is that it DEFINES a PROPERTY THAT the CLASSES CAN then IMPLEMENT.
9
+ In practice, the interface (which is a sort of CLASS ITSELF) TYPICALLY DEFINES a METHOD or METHODS WITHOUT actual IMPLEMENTATION.
10
+ The classes that implement the interface then implement the method(s) as well.
11
+
12
+ This enables the grouping of objects based on a single property,
13
+ even if the objects themselves are very different (for example a banana and an airplane).
14
+ This way we can for example calculate the total price of different objects with price by using a simple loop.
15
+
16
+
17
+
18
+ ========================================
19
+
20
+
21
+
22
+ Interfaces in Java
23
+ In Java, an interface is defined with the keyword 'interface'.
24
+ Inside the interface, ALL DECLARED MEMBERS are PUBLIC, because the implementing classes need to PROVIDE the MEMBERS TO their CLIENTS.
25
+
26
+ For example, see the interface 'Priced':
27
+ interface Priced {
28
+ int getPrice();
29
+ }
30
+
31
+
32
+ Note that getPrice() is a 'method declaration'.
33
+ VISIBILITY MODIFIERS are NOT NEEDED, since ALL the MEMBERS defined in the interface are PUBLIC.
34
+
35
+ As seen in the example, 'no implementations for the methods are given' in the interface.
36
+ The interface is implemented with keyword 'implements' (different from 'extends', which is used when we extend a class).
37
+
38
+ class Car implements Priced {
39
+ private String manufacturer;
40
+ private String model;
41
+ private int price;
42
+
43
+ public Car(String manufacturer, String model, int price) {
44
+ this.manufacturer = manufacturer;
45
+ this.model = model;
46
+ this.price = price;
47
+ }
48
+
49
+ // update method
50
+ @Override
51
+ public int getPrice() {
52
+ return price;
53
+ }
54
+ }
55
+
56
+
57
+
58
+
59
+
60
+ As a second example, let's look at class Banana.
61
+ This time the price is fixed at 5.
62
+ This does not make a difference, the interface just defines that the class needs to return an integer from the method getPrice().
63
+
64
+ class Banana implements Priced {
65
+ double length;
66
+
67
+ public Banana(double length) {
68
+ this.length = length;
69
+ }
70
+
71
+ @Override
72
+ public int getPrice() {
73
+ return 5;
74
+ }
75
+ }
76
+
77
+
78
+
79
+
80
+
81
+ The implementing classes can have a very different basic idea.
82
+ However, they need to implement the method getPrice() which is defined in the interface.
83
+
84
+ public class Test {
85
+ public static void main(String[] args) {
86
+ Car car = new Car("Volvo", "345", 1500);
87
+ Banana banana = new Banana(15.25);
88
+
89
+ System.out.println(car.getPrice());
90
+ System.out.println(banana.getPrice());
91
+ }
92
+ }
93
+
94
+ Program outputs:
95
+ 1500
96
+ 5
97
+
98
+
99
+
100
+
101
+ ========================================
102
+
103
+
104
+
105
+ In the newer versions of Java, interface methods can have default implementations.
106
+ This helps for example when you need to make changes in the interface.
107
+ The feature is not discussed in detail in this course, more information can be found e.g. at:
108
+ https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
109
+
110
+
111
+
112
+ ========================================
113
+
114
+ Let's see a second example, an interface called Measurable which defines methods getWidth and getHeight.
115
+
116
+ interface Measurable {
117
+ double getWidth();
118
+ double getHeight();
119
+ }
120
+
121
+
122
+
123
+ Now the implementing classes MUST IMPLEMENT BOTH METHODS:
124
+ class Block implements Measurable {
125
+ private String material;
126
+ private double width;
127
+ private double height;
128
+
129
+ public Block(String material, double width, double height) {
130
+ this.material = material;
131
+ this.width = width;
132
+ this.height = height;
133
+ }
134
+
135
+
136
+ ////////////////////
137
+ // HERE
138
+ ////////////////////
139
+ @Override
140
+ public double getWidth() {
141
+ return width;
142
+ }
143
+
144
+ @Override
145
+ public double getHeight() {
146
+ return height;
147
+ }
148
+ ////////////////////
149
+
150
+ public String getMaterial() {
151
+ return material;
152
+ }
153
+ }
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+ A class can implement more than one interface:
163
+ class Block implements Measurable, Priced {
164
+ private String material;
165
+ private double width;
166
+ private double height;
167
+
168
+ public Block(String material, double width, double height) {
169
+ this.material = material;
170
+ this.width = width;
171
+ this.height = height;
172
+ }
173
+
174
+ ////////////////////
175
+ // HERE
176
+ ////////////////////
177
+ @Override
178
+ public double getWidth() {
179
+ return width;
180
+ }
181
+
182
+ @Override
183
+ public double getHeight() {
184
+ return height;
185
+ }
186
+
187
+ @Override
188
+ public int getPrice() {
189
+ return 25;
190
+ }
191
+ ////////////////////
192
+
193
+ public String getMaterial() {
194
+ return material;
195
+ }
196
+ }
197
+
198
+
199
+