Skip to Content
Suffering builds character
아카이브10.Java특징추상화11. 추상 클래스의 구현체

11. 추상 클래스의 구현체

이러한 추상 클래스 Animal을 확장한 Mouse 클래스

Animal.java
public class Mouse extends Animal { // Error. }

Mouse 클래스로 Animal 클래스를 확장할 경우 아래와 같은 컴파일 에러가 발생함

컴파일 에러 메시지
"The type Mouse must implement the inherited abstract method Animal.sing()"

Mouse 타입(class)은 무조건 상속된 추상 메서드 Animal 타입(class)의 sing()을 구현해야함

이러한 에러는 앞서 10. 추상 클래스에서 class 앞에 abstract 키워드를 작성하지 않았을 때 발생한 에러와 유사한 맥락이라고 볼 수 있음

Main.java
Animal a = new Animal(); // Error. Cannot instantiate the type Animal

왜냐하면 현재 Mouse 클래스는 추상 클래스가 아니기 때문에 이대로 객체를 생성할 경우 특성으로 물려받은 미구현 메서드인 sing()을 호출할 수 있기 때문

Main.java
Mouse jerry = new Mouse(); jerry.sing(); // Error. 미구현 메서드 sing() 호출

따라서 추상 클래스를 확장할 경우 해당 클래스에서 요구하는(상속받은) 미구현된 메서드들을 모두 구현하도록 강제됨

1. 구현 클래스, Implementation

이러한 추상 클래스로부터 물려받은 미구현된 메서드들을 모두 구현한 클래스를 구현 클래스(implementation) 혹은 구현체라고 함

Mouse.java
public class Mouse extends Animal { // Error. @Override void sing() { System.out.println("찍찍"); } }

2. 추상 클래스, 추상 메서드 요약

결과적으로 Animal 클래스 내 sing()은 구현하지 않으면서 Animal을 확장한 구체적인 클래스(implementation)들은 자신만의 메서드 내용을 구현함

Animal.java
public abstract class Animal { void sing(); } class Mouse extends Animal { @Override void sing() { System.out.println("찍찍"); } }
  1. Animal 클래스는 추상 클래스이기 때문에 인스턴스(객체) 생성이 불가능해짐

  2. 다형성의 개념은 유지됨

Main.java
Animal mouse = new Mouse();
Last updated on