-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphism.java
More file actions
87 lines (65 loc) · 1.79 KB
/
Polymorphism.java
File metadata and controls
87 lines (65 loc) · 1.79 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
80
81
82
83
84
85
86
87
// Polymorphism...
class Aeroplane {
void takeOff() {
System.out.println("Plane is taking off!!");
}
void fly() {
System.out.println("Plane is flying!!");
}
void type() {
System.out.println("Platunum Plane!!");
}
}
class CargoPlane extends Aeroplane {
void fly() {
System.out.println("Plane is flying cargo!!");
}
void type() {
System.out.println("Metal Plane!!");
}
}
class PassengerPlane extends Aeroplane {
void fly() {
System.out.println("Plane is flying Passengers!!");
}
void type() {
System.out.println("golden Plane!!");
}
}
class FighterPlane extends Aeroplane {
void fly() {
System.out.println("Plane is flying fighters and ammonation!!");
}
void type() {
System.out.println("Black Stainless Steel Plane!!");
}
}
class Airport // Runtime Plymorphism...
{
void poly(Aeroplane aero) {
aero.fly();
aero.type();
System.out.println("-----------------------------------------------------------------");
}
}
class Polymorphism {
public static void main(String[] args) {
CargoPlane cp = new CargoPlane();
PassengerPlane pp = new PassengerPlane();
FighterPlane fp = new FighterPlane();
Airport air = new Airport();
air.poly(cp);
air.poly(pp);
air.poly(fp);
// cp = pp; (error: since both belongs to a different class )
// Aeroplane air;
// air = pp; // (air can hold pp since air is the parent class of pp)
// cp = air; // (error: child class can't hold parent class)
// Polymorphism - same air in two forms...
// air.fly();
// air.type();
// air = cp;
// air.fly();
// air.type();
}
}