결론 먼저 요약 :
* 추상화
* 다형성
* 재사용성
* 클래스 간 느슨한 결합
을 위해 쓴다.
아래는 Interface 관련 자세한 설명 부분.
항상 느끼는 것이지만 프로그래밍 관련 설명은 한글로 번역하면 더 어려운 것 같아 원문 그대로가 이해하기 오히려 더 쉬운 느낌.
예시로, 다형성...쉽게 생각하면 우리나라 '사과' 의 경우에도 단어는 '사과' 하나이지만 뜻은 과일 의미로 쓰일 때도 있고, 잘못한 것을 사과한다는 의미로도 쓰인다. 하나의 '단어(프로그래밍의 경우 변수,함수 등)'가 여러가지로 확장돼서 쓰일 수 있음을 뜻하는 것인데 '다형성' 이라고 너무 어렵게 표현되어 있어 거리감 느껴짐. 이런 여러 가지 이유 등으로 웬만하면 영어 원문으로 찾아보고 이해하는 게 더 빠른 느낌...
In Java, an interface is a reference type, similar to a class, but it is used to specify methods that a class must implement without providing their actual implementation. An interface defines a contract that any class implementing it must follow.
Key Characteristics of an Interface :
1. Method Declaration Only: An interface only contains method signatures (abstract methods). The implementing class must provide the method definitions.
Example:
**java code**
public interface Vehicle {
void start();
void stop();
}
2. No Fields (Except Constants): Interfaces can't have instance fields, but they can contain constants (variables declared with public static final).
3. Multiple Inheritance: A class can implement multiple interfaces, allowing for a form of multiple inheritance, which is not possible with regular classes in Java.
4. Default and Static Methods: Since Java 8, interfaces can also include default and static methods with implementations.
Example of a default method :
**java code**
public interface Vehicle {
void start();
void stop();
default void honk() {
System.out.println("Honking...");
}
}
Why Are Interfaces Used ?
1. Abstraction: Interfaces provide a way to define what a class must do but not how it should do it. This allows developers to create more flexible and reusable code.
2. Multiple Inheritance: A class can implement multiple interfaces, enabling a form of multiple inheritance, which is not allowed with classes (where you can only extend one class).
Example :
**java code**
public class Car implements Vehicle, Engine {
@Override
public void start() {
// Implementation here
}
@Override
public void stop() {
// Implementation here
}
}
3. Loose Coupling: Interfaces help reduce dependencies between classes. By programming to an interface rather than a concrete implementation, you can more easily switch between different implementations of the same interface without modifying the consuming code.
Example :
**java code**
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
@Override
public void stop() {
System.out.println("Car stopped");
}
}
public class Motorcycle implements Vehicle {
@Override
public void start() {
System.out.println("Motorcycle started");
}
@Override
public void stop() {
System.out.println("Motorcycle stopped");
}
}
// The consuming code can use either a Car or Motorcycle
Vehicle myVehicle = new Car(); // or new Motorcycle();
myVehicle.start();
4. Polymorphism: Interfaces allow for polymorphic behavior, meaning the same interface can be used to refer to objects of different implementing classes.
In summary, interfaces are used to define a contract for what a class should do, enabling abstraction, flexibility, and code reusability, all while promoting loose coupling and polymorphism.
'Work' 카테고리의 다른 글
| [C#, CSharp] IIS에서 TLS 1.2 버전 활성화 방법 (2) | 2024.09.20 |
|---|---|
| TLS 가 뭐야? (1) | 2024.09.20 |
| 기획 툴 정리 (0) | 2024.09.07 |
| 자기 검열 (2) | 2024.09.07 |
| mssql 특정 테이블 칼럼 전체 조회 쿼리 (0) | 2024.02.06 |