Spaces:
Running
Running
public static Priced mostExpensive(ArrayList<Priced> list) {...}
Browse files
Week 6: Methods of OO Programming/10A. Interface Class as Variable Type+++
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Although an object cannot be formed from an interface, an 'interface' can be used as a 'variable type'.
|
2 |
+
Such a variable can store a REFERENCE to 'ANY OBJECT THAT IMPLEMENTS the INTERFACE'.
|
3 |
+
However, note that ONLY OPERATIONS defined IN the INTERFACE can be called through such a variable.
|
4 |
+
|
5 |
+
|
6 |
+
Let’s examine a method named ‘mostExpensive’, which takes a list of ‘Priced’ type objects as a parameter.
|
7 |
+
The method finds the most expensive among these.
|
8 |
+
|
9 |
+
Below is a refresher on the interface class ‘Priced’:
|
10 |
+
|
11 |
+
interface Priced {
|
12 |
+
int givePrice();
|
13 |
+
}
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
The static method 'mostExpensive' now returns the element with the highest price from the given elements:
|
21 |
+
|
22 |
+
public static Priced mostExpensive(ArrayList<Priced> list) {
|
23 |
+
|
24 |
+
Priced mostExpensive = list.get(0);
|
25 |
+
for (Priced product : list) {
|
26 |
+
|
27 |
+
// 'product' object is from a class implementing the 'Priced' interface
|
28 |
+
// which can use the 'givePrice' method from the interface
|
29 |
+
if (product.givePrice() > mostExpensive.givePrice()) {
|
30 |
+
mostExpensive = product;
|
31 |
+
}
|
32 |
+
}
|
33 |
+
return mostExpensive;
|
34 |
+
}
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
The objects in the list can be of different types, the only REQUIREMENT is that EACH IMPLEMENTS the "Priced" interface.
|
44 |
+
Examples include the classes "Fruit", "Car" and "MovieTicket":
|
45 |
+
|
46 |
+
class Fruit implements Priced {
|
47 |
+
private String name;
|
48 |
+
|
49 |
+
public Fruit(String name) {
|
50 |
+
this.name = name;
|
51 |
+
}
|
52 |
+
|
53 |
+
@Override
|
54 |
+
public int givePrice() {
|
55 |
+
return 10;
|
56 |
+
}
|
57 |
+
}
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
class Car implements Priced {
|
62 |
+
private String brand;
|
63 |
+
private int price;
|
64 |
+
|
65 |
+
public Car(String brand, int price) {
|
66 |
+
this.brand = brand;
|
67 |
+
this.price = price;
|
68 |
+
}
|
69 |
+
|
70 |
+
@Override
|
71 |
+
public int givePrice() {
|
72 |
+
return price;
|
73 |
+
}
|
74 |
+
}
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
class MovieTicket implements Priced {
|
79 |
+
private String movie;
|
80 |
+
private int ageLimit;
|
81 |
+
private int price;
|
82 |
+
|
83 |
+
public MovieTicket(String movie, int ageLimit, int price) {
|
84 |
+
this.movie = movie;
|
85 |
+
this.ageLimit = ageLimit;
|
86 |
+
this.price = price;
|
87 |
+
}
|
88 |
+
|
89 |
+
@Override
|
90 |
+
public int givePrice() {
|
91 |
+
return price;
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
Let's create a few objects and call the method:
|
100 |
+
public static void main(String[] args) {
|
101 |
+
ArrayList<Priced> products = new ArrayList<>();
|
102 |
+
products.add(new Car("Mercedes", 15000));
|
103 |
+
products.add(new MovieTicket("Rambo 6", 21, 13));
|
104 |
+
products.add(new Fruit("Banana"));
|
105 |
+
|
106 |
+
Priced mostExpensive = mostExpensive(products);
|
107 |
+
}
|
108 |
+
|
109 |
+
|