1 changed files with 37 additions and 0 deletions
@ -0,0 +1,37 @@ |
|||||
|
abstract class Animal { |
||||
|
abstract void makeSound(); |
||||
|
} |
||||
|
|
||||
|
interface Swimmable { |
||||
|
void swim(); |
||||
|
} |
||||
|
|
||||
|
class Dog extends Animal implements Swimmable { |
||||
|
public void makeSound() { |
||||
|
System.out.println("Woof!"); |
||||
|
} |
||||
|
|
||||
|
public void swim() { |
||||
|
System.out.println("Dog is swimming!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Cat extends Animal { |
||||
|
public void makeSound() { |
||||
|
System.out.println("Meow!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
Animal myDog = new Dog(); |
||||
|
Animal myCat = new Cat(); |
||||
|
|
||||
|
myDog.makeSound(); |
||||
|
myCat.makeSound(); |
||||
|
|
||||
|
if (myDog instanceof Swimmable) { |
||||
|
((Swimmable) myDog).swim(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue