forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCat.java
More file actions
38 lines (23 loc) · 827 Bytes
/
Cat.java
File metadata and controls
38 lines (23 loc) · 827 Bytes
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
// Since Cat extends Animal it gets all of Animals fields and methods
// This is called inheritance
public class Cat extends Animal{
public Cat() {
}
// Overriding the Animal method
public String makeSound(){
return "Meow";
}
public static void main(String[] args) {
Animal fido = new Dog();
Animal fluffy = new Cat();
// We can have an array of Animals that contain more specific subclasses
// Any overridden methods are used instead because of polymorphism
Animal[] theAnimals = new Animal[10];
theAnimals[0] = fido;
theAnimals[1] = fluffy;
System.out.println("Fido says " + theAnimals[0].makeSound());
System.out.println("Fluffy says " + theAnimals[1].makeSound());
// We can also pass subclasses of Animal and they just work
speakAnimal(fluffy);
}
}