Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 위크맵
- react-slick
- Map
- Mongoose
- 자바스크립트
- JavaScript
- 이메일 전송
- 참조에 의한 객체 복사
- 구조 분해 할당
- 카카오로그인
- 카카오 소셜로그인
- TypeScript
- DB
- 위크셋
- 객체
- nodemailer
- AGGREGATE
- 로그스태시
- 화살표 함수
- nest
- logstash
- nextjs
- nestjs
- 중첩 구조 분해
- javacript
- context switch
- JSON.parse
- MongoDB
- JSON.stringify
- 캐러셀
Archives
- Today
- Total
뚜sh뚜sh
인터페이스(interface) 본문
추상 클래스와 인터페이스 비교
- 공통점
- 상위 클래스로만 사용 가능
- 하위에서 구현할 함수의 원형을 선언
- 차이점
1.
- 추상 클래스 = 일반 멤버 + 추상 메소드
- 인터페이스 = 추상 메소드 + 상수
2.
- 추상 클래스는 추상 메소드를 일부 구현 가능
- 인터페이스는 모두 구현해야 함(강제적)
3. 다중 상속 가능
4. 인터페이스가 인터페이스를 다중 상속
형식
- 기본 형식
interface 인터페이스명 {
public static final 데이터형 변수명 = 값;
public abstract 리턴형 메소드명(매개변수0, ...,n);
}
// 간소화한 버전
interface 인터페이스명 {
데이터형 변수명 = 값;
리턴형 메소드명(매개변수0, ...,n);
}
- 인터페이스의 상속 형식
interface 인터페이스명 {
데이터형 변수명 = 값;
리턴형 메소드명(매개변수0, ...,n);
}
class 클래스명 implements 인터페이스명 {
}
※ 인터페이스명 관례 : ~able
ex) Openable, Readable
package test2;
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
A Test = new A();
Test.Print();
}
}
interface InterfaceA {
int m_nVar = 7;
void Print();
}
class A implements InterfaceA {
public void Print() {
System.out.println("interface m_nVar = " + m_nVar);
}
}
인터페이스의 상속
1. 다중 상속
interface A {
void PrintA();
}
interface B {
void PrintB();
}
interface C extends A, B { // A, B 다중 상속
void PrintC();
}
package test2;
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
D Test = new D();
Test.PrintA();
Test.PrintB();
Test.PrintC();
}
}
interface A {
void PrintA();
}
interface B {
void PrintB();
}
interface C extends A, B { // A, B 다중 상속
void PrintC();
}
class D implements C {
public void PrintA() {
System.out.println("AAAAAAAA");
}
public void PrintB() {
System.out.println("BBBBBBBB");
}
public void PrintC() {
System.out.println("CCCCCCCC");
}
}
2. 클래스 상속과 인터페이스 다중 상속
package test2;
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
D Test = new D();
Test.PrintA();
Test.PrintB();
Test.PrintC();
}
}
interface A {
void PrintA();
}
interface B {
void PrintB();
}
class C {
public void PrintC() {
System.out.println("class C");
}
}
class D extends C implements A, B {
public void PrintA() {
System.out.println("class A");
}
public void PrintB() {
System.out.println("class B");
}
}
'Language > Java' 카테고리의 다른 글
내부 클래스와 익명 클래스 (0) | 2023.07.03 |
---|---|
예외 처리 (0) | 2023.07.03 |
추상 클래스(abstract class) (0) | 2023.07.03 |
상속(Inheritance) (0) | 2023.07.03 |
클래스 (0) | 2023.07.01 |
Comments