경주장
6주차 과제: 상속 #6 (~02.29) - 추상 클래스 본문
✔️ 학습할 것 (필수)
자바 상속의 특징메소드 오버라이딩final 키워드super 키워드다이나믹 메소드 디스패치 (Dynamic Method Dispatch)추상 클래스- Object 클래스
이번 주제는 오라클 자바 도큐먼트를 참고하여 정리하였습니다.
Def) Abstract class
An abstract class is a class that is declared 'abstract' -- it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
Def) Abstract method
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon)
e.g.) abstract void moveTo(double deltaX, double deltaY)
Notes!
- abstract method가 class에 선언되어 있다면 그 class는 abstract class여야 합니다..
- abstract method는 자식클래스를 가집니다. 일반적으로 자식 클래스는 부모 추상 클래스의 추상 메소드를 모두 구현합니다. 그렇지 않다면 자식 클래스 역시 추상클래스로 선언되어야 합니다.
- 인터페이스의 메소드는 모두 abstract입니다. (자동적으로 public abstract 키워드가 추가됨)
Abstract Class Compared to Interfaces
해당 주제에 관해서는 지난번에도 한번 포스팅을 하였는데 오라클 도큐먼트의 의견도 한번 정리하겠습니다.
추상클래스와 인터페이스의 차이
AVA 8 이후에는 구현을 포함한 public method를 Interface에 포함할 수 있습니다. (default method, static method) JAVA 9 이후에는 private method를 Interface에 포함할 수 있습니다. Interface와 Abstract Cla..
running-cheetah.tistory.com
추상 클래스 vs 인터페이스
공통점
- You cannot instantiate then
- they may contain a mix of methods declared with or without an implementation
차이점
- with abstract classes, you can declare fields that are not static and fianl
- with abstract classes, you can define public protected, and private concrete method
- you can extend only one class
- with interfaces, all fields are automatically public, static, and final (i.e. constant)
- with interfaces, all methods that you declare or define(as default methods) are public
- you can implement any number of interfaces
그렇다면 무엇을 써야 하는가?
도큐먼트는 아래와 같은 가이드라인을 제시합니다.
다음 중 하나라도 적용된다면 Abstract Classes를 사용하는것을 고려하세요
- 클래스 간에 코드를 공유(재활용) 하고 싶을때
- 많은 공통 메소드나 필드를 공유하는 Classes의 생성이 필요할때
- 혹은 protected와 private과 같은 접근제어자를 활용하고자 할때
- 상수가 아닌 필드가 필요할때
다음 중 하나라도 적용된다면 Interfaces 를 사용하는것을 고려하세요
- 로직에 크게 관여 없지만 여기저기서 불려서 구현 되어야 하는 경우 (e.g. Comparable, Clonable)
- 구현에 크게 관심이 없을때(?)
- 다중 상속(구현)의 메리트를 살리고 싶을때
@출처
Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritanc
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
'JAVA > whiteship_live-study' 카테고리의 다른 글
7주차 과제: 패키지 #7 패키지란 (~02.30) (0) | 2022.02.23 |
---|---|
6주차 과제: 상속 #6 (~02.29) - Object 클래스 (0) | 2022.02.23 |
6주차 과제: 상속 #6 (~02.29) - 메소드 디스패치 (0) | 2022.02.23 |
6주차 과제: 상속 #6 (~02.29) - final, super 키워드 (0) | 2022.02.23 |
6주차 과제: 상속 #6 (~02.29) - 메소드 오버라이딩 (0) | 2022.02.22 |