java用继承的方式实现一个多态程序
时间: 2023-05-23 22:05:15 浏览: 124
java继承多态
public class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class Cat extends Animal {
@Override
public void sound() {
System.out.println("Cat meows");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.sound();
animal2.sound();
}
}
// Output:
// Dog barks
// Cat meows
阅读全文