polymorphism completed
This commit is contained in:
@ -1,9 +1,14 @@
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
Dog d = new Dog("buster");
|
||||
Cat c = new Cat("mittens");
|
||||
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||
Animal[] animals = {
|
||||
new Dog("buster"),
|
||||
new Cat("mittens")
|
||||
};
|
||||
|
||||
d.make_sound();
|
||||
c.make_sound();
|
||||
for (Animal a : animals) {
|
||||
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||
a.make_sound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
Relevant files:
|
||||
|
||||
- [Program.java](./Program.java)
|
||||
- [Animal.java](./Animal.java)
|
||||
- [Cat.java](./Cat.java)
|
||||
- [Dog.java](./Dog.java)
|
||||
|
Reference in New Issue
Block a user