이번에는 클래스 상속과 인터페이스 구현을 함께 사용하는 예를 알아보자.
package poly.ex7;
public abstract class AbstractAnimal {
public abstract void sound();
public void move() {
System.out.println("이동");
};
}
package poly.ex7;
public interface Flyable {
void fly();
}
package poly.ex7;
public class Dog extends AbstractAnimal {
@Override
public void sound() {
System.out.println("멍멍");
}
@Override
public void move() {
System.out.println("강아지 이동");
}
}
package poly.ex7;
public class Bird extends AbstractAnimal implements Flyable{
@Override
public void sound() {
System.out.println("짹짹");
}
@Override
public void fly() {
System.out.println("날다");
}
}
package poly.ex7;
public class Chicken extends AbstractAnimal implements Flyable {
@Override
public void sound() {
System.out.println("꼬끼오");
}
@Override
public void fly() {
System.out.println("날다");
}
}
package poly.ex7;
public class AbstractMain {
public static void main(String[] args) {
AbstractAnimal[] animals = {new Dog(), new Bird(), new Chicken()};
for (AbstractAnimal animal : animals) {
animal.sound();
animal.move();
}
}
}
멍멍
강아지 이동
짹짹
이동
꼬끼오
이동