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.
 
 
 

23 lines
759 B

public class Main {
public static void main(String[] args) {
// Test polymorphism with Animal references
Animal dog = new Dog();
Animal cat = new Cat();
System.out.println("Testing makeSound():");
dog.makeSound(); // Should output "Woof!"
cat.makeSound(); // Should output "Meow!"
// Test swim() method for Dog
System.out.println("\nTesting swim():");
if (dog instanceof Swimmable) {
Swimmable swimmableDog = (Swimmable) dog;
swimmableDog.swim(); // Should output "Dog is swimming"
}
// Test that Cat doesn't implement Swimmable
if (!(cat instanceof Swimmable)) {
System.out.println("Cat cannot swim");
}
}
}