에러 모음

does not override abstract

junani0v0 2024. 4. 5. 16:31

public abstract class Animal {
    public abstract void sound();
}
public class Cat extends Animal{}
public class AbstractMain {
    public static void main(String[] args) {
        Cat cat = new Cat();
        soundAnimal(cat);
    }
    private static void soundAnimal(Animal animal) {
        System.out.println("동물 소리 테스트 시작");
        animal.sound();
        System.out.println("동물 소리 테스트 종료");
    }
}

 

< 에러 메시지 >

java: poly.ex.Cat is not abstract and does not override abstract method sound() in poly.ex.Animal

 

< 원인 >

추상 메서드 사용시 반드시 오버라이딩 해야하는데 오버라리딩 메서드를 만들지 않아 발생하는 컴파일 오류

(추상 메서드 덕분에 자식 클래스를 만들때 실수로 메서드 오버라이딩을 하지 않는 문제를 근본적으로 방지)

 

< 해결 >

자식 클래스에서 추상 메서드 오버라이딩을 작성

public class Cat extends Animal{
    @Override
    public void sound() {
        System.out.println("냐옹");
    }
}

 

 

Tip. 추상 클래스는 생성이 불가

생성시 아래와 같은 컴파일 오류 발생

< 에러 메시지 >

java: poly.ex.Animal is not abstract ; cannot be instantiated 

 

< 해결 >

생성한 추상 클래스 삭제

'에러 모음' 카테고리의 다른 글

AssertionFailedError  (0) 2024.04.08
MissingServletRequestParameterException  (0) 2024.04.08
ClassCastException  (0) 2024.04.02
method does not override  (0) 2024.04.01
java.lang.StringIndexOutOfBoundsException 발생  (0) 2024.03.29