티스토리 뷰
목표
자바의 인터페이스에 대해 학습하세요.
학습할 것 (필수)
- 인터페이스 정의하는 방법
- 인터페이스 구현하는 방법
- 인터페이스 레퍼런스를 통해 구현체를 사용하는 방법
- 인터페이스 상속
- 인터페이스의 기본 메소드 (Default Method), 자바 8
- 인터페이스의 static 메소드, 자바 8
- 인터페이스의 static 메소드, 자바 9
1. 인터페이스 정의하는 방법
interface <inteface_name> {
// constant fields
// public static final 생략가능
public static final int PI = 3.14;
// methods that abstract
// public static 생략 가능
public abstract void print();
}
인터페이스를 왜 사용하는가 ?
- 전체적인 추상화를 이루기 위해서 사용된다.
- Java 의 경우 다중 상속을 지원하지 않지만 인터페이스를 이용하면 다중 상속을 구현할 수 있다.
- 추상화를 구현하기 위해 사용되는데 그렇다면 왜 추상 클래스를 놔두고 인터페이스를 사용할까 ?
- 추상 클래스는 non-final 변수가 포함될 수 있지만 인터페이스의 변수는 final 이기 때문이다.
2. 인터페이스 구현하는 방법
public interface Animal {
default void run() {
System.out.println("다그닥다드각");
}
void cry();
}
// 구현
class Dog implements Animal {
@Override
public void cry() {
System.out.println("크르르르르르릉");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.cry();
dog.run();
// 일회용 객체
Animal animal = new Animal() {
@Override
public void cry() {
System.out.println("히이이잉~");
}
};
animal.cry();
}
}
기본적으로 "implements" 키워드를 사용한다.
인터페이스는 인스턴스화 할 수 없지만 익명 객체를 통해 일회용 객체를 만들 수 있다.
3. 인터페이스 레퍼런스를 통해 구현체를 사용하는 방법
4. 인터페이스 상속
interface In1 extends Test1 {
// 이런식으로 인터페이스도 상속이 가능하다
}
5. 인터페이스의 기본 메소드 (Default Method), 자바 8
6. 인터페이스의 static 메소드, 자바 8
7. 인터페이스의 static 메소드, 자바 9
자바 8 이전의 경우에서는 Constant variables, Abstarct methods 만 가질 수 있었다.
이때는 무조건 인터페이스를 구현한 클래스는 모든 메소드를 무조건 구현해야만 한다.
public interface TempI {
public abstract void method(int n);
}
class Temp implements TempI {
@Override
public void method(int n) {
if (n % 2 == 0)
System.out.println("geeksforgeeks");
else
System.out.println("GEEKSFORGEEKS");
}
public static void main(String[] args) {
TempI in1 = new Temp();
TempI in2 = new Temp();
in1.method(4);
in2.method(3);
}
}
// geeksforgeeks
// GEEKSFORGEEKS
자바 8 에서는 Default methods 와 Static methods 가 도입되었고 4가지 유형을 가질 수 있다.
1. Constant variables
2. Abstract methods
3. Default methods
4. Static methods
public interface TempI {
public abstract void div(int a, int b);
public default void add(int a, int b) {
System.out.print("Answer by Default method = ");
System.out.println(a + b);
}
public static void mul(int a, int b) {
System.out.print("Answer by Static method = ");
System.out.println(a * b);
}
}
class Temp implements TempI {
@Override
public void div(int a, int b) {
System.out.print("Answer by Abstract method = ");
System.out.println(a / b);
}
public static void main(String[] args) {
TempI in = new Temp();
in.div(8, 2);
in.add(3, 1);
TempI.mul(4, 1);
}
}
// Answer by Abstract method = 4
// Answer by Default method = 4
// Answer by Static method = 4
자바 9 는 인터페이스에 private methods 와 private static methods 를 도입했고 6가지 유형을 가질 수 있다.
1. Constant variables
2. Abstract methods
3. Default methods
4. Static methods
5. Private methods
6. Prviate Static methods
이러한 private methods 는 인터페이스 내부의 코드 재사용성을 향상시킨다.
private 메서드는 해당 인터페이스 내에서만 접근할 수 있으며 다른 인터페이스로 접근이나 클래스로 구현할 수 없다.
public interface TempI {
public abstract void mul(int a, int b);
public default void add(int a, int b) {
// private method inside default method
sub(a, b);
// static method inside other non-static method
div(a, b);
System.out.print("Answer by Default method = ");
System.out.println(a + b);
}
public static void mod(int a, int b) {
div(a, b);
// static method inside other static method
System.out.print("Answer by Static method = ");
System.out.println(a % b);
}
private void sub(int a, int b) {
System.out.print("Answer by Private method = ");
System.out.println(a - b);
}
private static void div(int a, int b) {
System.out.print("Answer by Private static method = ");
System.out.println(a / b);
}
}
class Temp implements TempI {
@Override
public void mul(int a, int b) {
System.out.print("Answer by Abstract method = ");
System.out.println(a * b);
}
public static void main(String[] args) {
TempI in = new Temp();
in.mul(2, 3);
in.add(6, 2);
TempI.mod(5, 3);
}
}
// Answer by Abstract method = 6 // mul(2, 3) = 2*3 = 6
// Answer by Private method = 4 // sub(6, 2) = 6-2 = 4
// Answer by Private static method = 3 // div(6, 2) = 6/2 = 3
// Answer by Default method = 8 // add(6, 2) = 6+2 = 8
// Answer by Private static method = 1 // div(5, 3) = 5/3 = 1
// Answer by Static method = 2 // mod(5, 3) = 5%3 = 2
※ 출처
※ 스터디
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- js promise
- 이펙티브자바 아이템60
- 이펙티브자바 아이템59
- js array
- dreamcoding
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- 킹수빈닷컴
- JPA 연관관계 매핑
- 김영한 JPA
- 백준
- 드림코딩
- http
- 프로그래머스 SQL
- Spring Security
- JS 딥다이브
- 백기선 스터디
- java
- GCP
- HTTP 완벽 가이드
- HTTP 완벽가이드
- 패스트캠퍼스 컴퓨터공학 완주반
- js api
- 프로그래머스
- BOJ
- REST API
- ㅇㄷㅇㅈ
- 모던자바스크립트
- 이펙티브자바
- 김영한 http
- 이펙티브자바 스터디
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함