You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
731 B
37 lines
731 B
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();
|
|
}
|
|
}
|
|
}
|