-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimal.java
49 lines (45 loc) · 944 Bytes
/
Animal.java
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
class Animal{
public void eat(){
System.out.println("Animal eat");
}
}
interface Pet{
public abstract void beFriendly( );
}
class Dog extends Animal implements Pet{
public void eat(){
System.out.println("Dog eat");
}
public void bark(){
System.out.println("Dog bark");
}
public void beFriendly(){
System.out.println("Dog is Friendly");
}
}
class Beagle extends Dog{
public void eat(){
System.out.println("Beagle eat");
}
public void bark(){
System.out.println("Beagle bark");
}
public void beFriendly(){
System.out.println("Beagle is Friendly");
}
}
class CastTest{
public static void main(String[] args){
Animal [] a = {new Animal(), new Dog(), new Animal(), new Beagle() };
Dog dAnimal;
System.out.println("Various Animal behaviours");
for(Animal aAnimal : a){
aAnimal.eat();
if( aAnimal instanceof Dog){
dAnimal = (Dog) aAnimal;
dAnimal.bark();
dAnimal.beFriendly();
}
}
}
}