-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
49 lines (37 loc) · 1.03 KB
/
Copy pathApp.java
File metadata and controls
49 lines (37 loc) · 1.03 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
package tutorial23;
class Machine {
public void start() {
System.out.println("Machine started");
}
}
class Camera extends Machine {
public void start() {
System.out.println("Camera Started");
}
public void snap() {
System.out.println("Photo Taken");
}
}
public class App {
public static void main(String[] args) {
Machine machine1 = new Machine();
Camera camera1 = new Camera();
machine1.start();
camera1.start();
camera1.snap();
// upcasting .. casted to class higher in hierarchy
Machine machine2 = camera1;
machine2.start();
// machine2.snap(); type of var determines methods avail
// type of obj determines which method is called though
Machine machine3 = new Camera();
Camera camera2 = (Camera)machine3;
camera2.start();
camera2.snap();
// doesn't work --- runtime error
Machine machine4 = new Machine();
//Camera camera3 = (Camera)machine3;
// Camera3.start();
// Camera3.snap();
}
}