-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTestAnimal.java
More file actions
38 lines (32 loc) · 1.02 KB
/
TestAnimal.java
File metadata and controls
38 lines (32 loc) · 1.02 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
package src.ex4;
public class TestAnimal {
public static void main(String[] args)
{
// Using the subclasses
Cat cat1 = new Cat();
cat1.greeting();
Dog dog1 = new Dog();
dog1.greeting();
BigDog bigDog1 = new BigDog();
bigDog1.greeting();
// Using Polymorphism
Animal animal1 = new Cat();
animal1.greeting();
Animal animal2 = new Dog();
animal2.greeting();
Animal animal3 = new BigDog();
animal3.greeting();
// Animal animal4 = new Animal(); // Error!!! Animal is abstract; cannot be instantiated !
// Downcast
Dog dog2 = (Dog)animal2;
BigDog bigDog2 = (BigDog)animal3;
Dog dog3 = (Dog)animal3;
// Cat cat2 = (Cat)animal2; // Error!!! Dog cannot be cast to Cat !
dog2.greeting(dog3);
dog3.greeting(dog2);
dog2.greeting(bigDog2);
bigDog2.greeting(dog2);
bigDog2.greeting(bigDog1);
bigDog1.greeting(bigDog2);
}
}