public static abstract class Vehicle{
public String name;
public void ride(){
System.out.println("달리기");
}
public abstract int numOfWheel();
}
public static class Car extends Vehicle{
public String name;
public Car(){
this.name = name;
}
@Override
public int numOfWheel(){
System.out.println(4);
return 4;
}
}
public static void main(String args[]) {
Car car = new Car();
car.ride(); //달리기
car.numOfWheel(); //4
}
만약 다중 상속이 가능하다고 하면,
public abstract class Person {
public abstract void speak();
}
public class Father extends Person {
@Override
public void speak(){
System.out.println("speak implementation of Father");
}
}
public class Mother extends Person {
@Override
public void speak(){
System.out.println("speak implementation of Mother");
}
}
public class Child extends Father, Mother {
public void test(){
speak();
}
}
Child가 Father와 Mother를 동시에 상속받는 상태에서 speak()메소드를 실행할 때,
speak() 메소드가 Father와 Mother 중 누구에게 상속받은 메소드인지 모호하게 된다.
public interface VehicleAction{
void ride();
void stop();
}
public class Car implements VehicleAction{
@Override
public void ride(){
...
}
public void stop(){
...
}
}
default : 인터페이스 내에서 직접 메소드를 구현할 때 사용하는 접근 제어자
- 오버라이딩을 해도 되고 안해도 됨
특징 | 추상 클래스 | 인터페이스 |
---|---|---|
사용 의도 | 여러 클래스의 공통점을 찾아 추상화(is-a) | 여러 구현 객체가 같은 동작을 한다는 것을 보장(has - a) |
다중 상속 | 불가능 | 가능 |
출처
https://limkydev.tistory.com/188
https://velog.io/@codemcd/%EC%9D%B8%ED%84%B0%ED%8E%98%EC%9D%B4%EC%8A%A4Interface
https://myjamong.tistory.com/150
https://jeong-pro.tistory.com/82
https://readystory.tistory.com/112