forked from icterguru/JavaProgrammingA2Z
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractClassEx01.java
More file actions
81 lines (64 loc) · 1.86 KB
/
AbstractClassEx01.java
File metadata and controls
81 lines (64 loc) · 1.86 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
package chapter11;
/* AbstractClassEx01.java */
/* Inheriting Abstract Base class/ Abstract Member */
abstract class Mammal extends Object{
abstract public void move();
public void nonAbstractMethod()
{
System.out.println("Mammal::nonAbstractMethod()");
// ...
}
}
class Human extends Mammal{
public void move(){
System.out.println("Human can't fly but can walk and run");
}
public void talk(){
System.out.println("Human can talk");
}
@Override
public void nonAbstractMethod()
{
System.out.println("Human::nonAbstractMethod()");
}
}
class Bird extends Mammal{
@Override
public void move(){
System.out.println("Birds can walk, fly, and run");
}
public void fly(){
System.out.println("Birds can fly");
}
@Override
public void nonAbstractMethod()
{
System.out.println("Bird::nonAbstractMethod()");
// ...
}
}
public class AbstractClassEx01{
public static void main(String args[]){
Human h0 = new Human(); // Human reference and Human object
// Here h0 is a Human object that references to a Human type
Human h1 = h0; // Human reference and Human object
// Here h1 and h0 both reference to a Human type
Mammal h2 = new Human(); // Mammal reference and Mammal object
// Here h2 is a Human object that references to a Mammal type
Bird b1 = new Bird(); // Birds reference and Birds object
// Here b1 is a Bird object that references to a Birds type
Mammal b2 = new Bird(); // Mammals reference and Birds object
// Here h2 is a Bird object that is a reference to a Mammal type
b1.move();
b1.fly();
b1.nonAbstractMethod();
h1.move();
h1.talk();
h1.nonAbstractMethod();
h2.move();
// h2.talk(); // Not allowed
h2.nonAbstractMethod();
b2.move();
// b2.fly(); // Not allowed
}
}