10 changed files with 46 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,3 @@ |
|||
public abstract class Animal { |
|||
public abstract void makeSound(); |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,23 @@ |
|||
public class AnimalTest { |
|||
public static void main(String[] args) { |
|||
Animal[] animals = new Animal[2]; |
|||
animals[0] = new Dog(); |
|||
animals[1] = new Cat(); |
|||
|
|||
for (Animal a : animals) { |
|||
a.makeSound(); |
|||
if (a instanceof Swimmable) { |
|||
((Swimmable) a).swim(); |
|||
} |
|||
} |
|||
|
|||
System.out.println("\n--- Testing individual Dog ---"); |
|||
Dog dog = new Dog(); |
|||
dog.makeSound(); |
|||
dog.swim(); |
|||
|
|||
System.out.println("\n--- Testing individual Cat ---"); |
|||
Cat cat = new Cat(); |
|||
cat.makeSound(); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,6 @@ |
|||
public class Cat extends Animal { |
|||
@Override |
|||
public void makeSound() { |
|||
System.out.println("Cat says: Meow! Meow!"); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,11 @@ |
|||
public class Dog extends Animal implements Swimmable { |
|||
@Override |
|||
public void makeSound() { |
|||
System.out.println("Dog says: Woof! Woof!"); |
|||
} |
|||
|
|||
@Override |
|||
public void swim() { |
|||
System.out.println("Dog is swimming"); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,3 @@ |
|||
public interface Swimmable { |
|||
void swim(); |
|||
} |
|||
Loading…
Reference in new issue