-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses.java
More file actions
62 lines (52 loc) · 1.11 KB
/
Classes.java
File metadata and controls
62 lines (52 loc) · 1.11 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
import java.util.ArrayList;
import java.util.List;
public class Classes {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
List<Animal> animals = new ArrayList<>();
animals.add(dog);
animals.add(cat);
for (Animal animal : animals) {
System.out.println(animal.toString());
animal.communicate();
}
}
abstract static class Animal {
protected static long ID = 1L;
protected String sound = null;
public void communicate() {
System.out.println("The animal makes no sound");
}
@Override
public String toString() {
return "Animal";
}
}
static class Dog extends Animal {
public Dog() {
sound = "bark";
}
@Override
public void communicate() {
System.out.println("The dog " + sound + "s.");
}
@Override
public String toString() {
return super.toString() + "::Dog";
}
}
static class Cat extends Animal {
public Cat() {
sound = "meow";
}
@Override
public void communicate() {
System.out.println("The cat " + sound + "s.");
}
@Override
public String toString() {
return super.toString() + "::Cat";
}
}
}