2020-05-24 17:04:37 +02:00
|
|
|
public class Program {
|
|
|
|
public static void main(String[] args) {
|
2020-05-24 18:44:11 +02:00
|
|
|
// 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")
|
|
|
|
};
|
2020-05-24 17:04:37 +02:00
|
|
|
|
2020-05-24 18:44:11 +02:00
|
|
|
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();
|
|
|
|
}
|
2020-05-24 17:04:37 +02:00
|
|
|
}
|
|
|
|
}
|