10 changed files with 37 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,6 @@ |
|||
public class Cat extends Animal { |
|||
@Override |
|||
public void makeSound() { |
|||
System.out.println("Meow!"); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,11 @@ |
|||
public class Dog extends Animal implements Swimmable { |
|||
@Override |
|||
public void makeSound() { |
|||
System.out.println("Woof!"); |
|||
} |
|||
|
|||
@Override |
|||
public void swim() { |
|||
System.out.println("Dog is swimming"); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
Animal[] animals = new Animal[2]; |
|||
animals[0] = new Dog(); |
|||
animals[1] = new Cat(); |
|||
|
|||
for (Animal animal : animals) { |
|||
animal.makeSound(); |
|||
if (animal instanceof Swimmable) { |
|||
((Swimmable) animal).swim(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,3 @@ |
|||
public interface Swimmable { |
|||
void swim(); |
|||
} |
|||
Loading…
Reference in new issue