-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestInterfaces.java
More file actions
79 lines (65 loc) · 2.11 KB
/
testInterfaces.java
File metadata and controls
79 lines (65 loc) · 2.11 KB
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
public class testInterfaces{
public static void main (String[] args){
Car my_car=new Car();
MBike my_bike=new MBike();
// // We cannot do this:
// Drivable something=new Drivable(); // which Drivable to construct? No constructor exists!
// But we can do this: an array of "drivables"
Drivable[] garage=new Drivable[2];
// And store drivables in this array (implicit upcasting):
garage[0]=my_car;
garage[1]=my_bike;
// We can do also this:
Drivable v; // allowed: v can contain a reference to any Drivable, but right now does not contain anything.
System.out.println("Taking the first vehicle for a test drive:");
v=garage[0];
driveVehicle(v);
System.out.println("Taking the second vehicle for a test drive:");
v=garage[1];
driveVehicle(v);
}
static void driveVehicle(Drivable vehicle) {
System.out.println("Starting your vehicle");
vehicle.speedUp(10);
System.out.println("Stopping your vehicle");
vehicle.brake();
System.out.println("Happy driving!");
}
}
interface Drivable{
public void speedUp(double increment);
public void brake();
}
interface SeatAdjustable{
public void liftSeat(double increaseHeight);
}
class Car implements Drivable{
private double speed;
public void speedUp(double increment){
speed += increment;
System.out.println("Speeding the car, speed="+speed);
}
public void brake() {
System.out.println("Stopping the car");
speed=0;
}
}
class MBike implements Drivable, SeatAdjustable{
private double seatHeight;
private double cadence;
private double gearCoeff=1;
public void setGear(double coeff) {
gearCoeff=coeff;
}
public void speedUp(double increment) {
cadence += increment/gearCoeff;
System.out.println("Biking with cadence="+cadence);
}
public void brake() {
System.out.println("Stopping the bike");
cadence=0;
}
public void liftSeat(double h) {
seatHeight += h;
}
}