-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstraction.java
More file actions
61 lines (53 loc) · 1.25 KB
/
Abstraction.java
File metadata and controls
61 lines (53 loc) · 1.25 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
abstract class Aeroplane
{
void takeOff()
{
System.out.println("Plane is taking off!!");
}
abstract void fly();
abstract void type();
}
class CargoPlane extends Aeroplane
{
void fly()
{
System.out.println("Plane is flying cargo!!");
}
void type()
{
System.out.println("Metal Plane!!");
}
void alert()
{
System.out.println("Alert crash!!!");
}
}
class PassengerPlane extends Aeroplane
{
void fly()
{
System.out.println("Plane is flying Passengers!!");
}
void type()
{
System.out.println("golden Plane!!");
}
}
public class Abstraction {
public static void main(String []args)
{
Aeroplane cp = new CargoPlane();
cp.fly();
cp.type();
cp.takeOff();
// cp.alert();
// (error: cannot call specialised method in cp since its refernce is Aeroplane)
((CargoPlane)cp).alert(); // Solution
// down casting - to use a specialised method in cp we make it temporarily of cp class
Aeroplane pp = new PassengerPlane();
pp.fly();
pp.type();
pp.takeOff();
// Aeroplane ap = new Aeroplane(); //(error: we cannot create object of an abstract class)
}
}