JAVA

interface vs abstract class

icedstone 2025. 7. 5. 14:43
반응형

Oracle java tutorial 확인시 다음과 같이 설명하고 있다.

  • Consider using abstract classes if any of these statements apply to your situation:
    • You want to share code among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
  • Consider using interfaces if any of these statements apply to your situation:
    • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
    • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
    • You want to take advantage of multiple inheritance of type.

 

상태/공통 기능이 필요한 경우에는 abstract class

기능 계약, 다중 구현이 필요한 경우에는 interface

이렇게 사용하면 된다는 의미 같은데 여전히 잘 모르겠다. Stack over flow에 나와있는 내용을 보면 다음과 같이 생각하면 쉽다고 한다.

 

할 수 있는 기능을 정의: interface

어떤 종류의 class: abstract class

 

여전히 모르겠다. 구성도로 살펴보자.

 

출처: http://xyzcode.blogspot.com/2016/04/use-abstract-classes-and-interfaces.html

새(Bird)는 날 수 있는 (Flyable) 동물(Animal) 이다.

항공기(Aircraft)는 날 수 있는 (Flyable) 탈 것(Vehicle)이다.

 

 

반응형

'JAVA' 카테고리의 다른 글

Java IO vs NIO  (0) 2025.07.20
Java Collection Framework  (4) 2025.07.12
Blocking vs Non-Blocking, Synchronous vs Asynchronous  (0) 2025.04.21
JVM 메모리 구조  (0) 2025.04.19
JVM GC(Garbage Collection)  (0) 2025.04.19