Backend/JAVA2 멘토시리즈

JAVA Eclipse 27 인터페이스

쏠솔랄라 2023. 5. 12. 17:31

 

 

인터페이스

 

: inter(사이에)+face(마주하다)의 합성어로 물체들 사이에서 상호작용 할 수 있도록 하는 매개 역할

 

클래스 : 설계도 / 인터페이스 : 규격

 

 

인터페이스의 멤버

상수 : final 타입 변수명=값; <-/->변수 사용불가

추상 메서드 : abstract 리턴 타입 메서드 명();

 

 

인터페이스 구현 방법

interface 인터페이스명 {
필드
}

 

 

클래스로 인터페이스 구현

interface 인터페이스명 {

}

class 클래스명 implements 인터페이스명 {
     추상 메서드 구현
}

 

 

ex.

public class Interface {

 

public static void main(String[] args) {

 

B b = new B();

 

b.methodA();

b.methodB();

 

}

}

interface Ainter {

final int x=10;

int y=20; // final 생략 가능 -> 상수이다

 

abstract void methodA();

void methodB(); // abstract생략 가능

}

class B implements Ainter {

 

@Override

public void methodA() {

System.out.println("methodA()");

}

 

@Override

public void methodB() {

System.out.println("methodB()");

}

 

}

 

 

인터페이스 주의할 점

 

인터페이스 내 일반 메서드 불가

인터페이스 내 변수 불가(상수만 가능)

인터페이스로 객체 생성 불가

인터페이스 내 멤버 메서드 abstract 키워드 생략 가능

인터페이스 내 멤버 변수 final 키워드 생략 가능

 

 


 

 

Exercise 1

 

public class Interface2 {

 

public static void main(String[] args) {

 

TourGuide guide = new TourGuide();

 

guide.leisureSports();

guide.sightseeing();

guide.food();

 

}

}

 

interface Providable {

void leisureSports();

void sightseeing();

void food();

}

 

class KoreaTour implements Providable {

 

@Override

public void leisureSports() {

System.out.println("한강 수상스키");

}

 

@Override

public void sightseeing() {

System.out.println("경복궁 관람 투어");

}

 

@Override

public void food() {

System.out.println("전주 비빔밥 투어");

}

}

class TourGuide {

private Providable tour = new KoreaTour();

 

public void leisureSports() {

tour.leisureSports();

}

 

public void sightseeing() {

tour.sightseeing();

}

 

public void food() {

tour.food();

}

}

 

+

class JapanTour implements Providable {

 

@Override

public void leisureSports() {

System.out.println("도쿄타워 번지점프");

}

 

@Override

public void sightseeing() {

System.out.println("오사카 시내 투어");

}

 

@Override

public void food() {

System.out.println("초밥 투어");

}

}

 

// JapanTour 를 추가하고 싶을 때 동일한 방법으로 클래스를 생성하고 인터페이스를 상속받는다

 

 


 

 

Exercise 2

 

public class Interface3 {

 

public static void main(String[] args) {

 

MyCellPhone phone1 = new MyCellPhone();

Camera phone2 = new MyCellPhone();

Call phone3 = new MyCellPhone();

Memo phone4 = new MyCellPhone();

Clock phone5 = new MyCellPhone();

PhoneUser user = new PhoneUser();

 

user.call(phone1);

// user.call(phone2);

user.call(phone3);

// user.call(phone4);

// user.call(phone5);

 

phone1.calling();

phone1.write();

phone2.photo();

phone3.calling();

phone4.write();

phone5.clock();

 

 

}

}

interface Camera {

void photo();

}

interface Call {

void calling();

}

interface Memo {

void write();

}

interface Clock {

void clock();

}

class MyCellPhone implements Camera, Call, Memo, Clock {

 

@Override

public void photo() {

System.out.println("photo()");

}

 

@Override

public void calling() {

System.out.println("calling()");

}

 

@Override

public void write() {

System.out.println("write()");

}

 

@Override

public void clock() {

System.out.println("clock()");

}

}

class PhoneUser {

void call (Call c) {

System.out.println("전화를 걸었습니다.");

}

}

 

 


 


JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

JAVA Eclipse 09 제어문 : 반복문 for
https://developernew.tistory.com/102

JAVA Eclipse 10 제어문 : 반복문 while, do-while
https://developernew.tistory.com/103

JAVA Eclipse 11 제어문 : 반복문의 break, continue
https://developernew.tistory.com/106

JAVA Eclipse 12 배열 : 배열의 개념 및 사용
https://developernew.tistory.com/107

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기
https://developernew.tistory.com/115

JAVA Eclipse 14 배열 : 다차원배열
https://developernew.tistory.com/117

JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120

JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기
https://developernew.tistory.com/124

JAVA Eclipse 17 method 메서드(메소드)
https://developernew.tistory.com/126

JAVA Eclipse 18 메서드(메소드) 오버로딩
https://developernew.tistory.com/133

JAVA Eclipse 19 객체지향 언어
https://developernew.tistory.com/135

JAVA Eclipse 20 클래스와 객체
https://developernew.tistory.com/155

JAVA Eclipse 21 인스턴스 변수와 클래스 변수
https://developernew.tistory.com/156

JAVA Eclipse 22 객체 타입 배열
https://developernew.tistory.com/157

JAVA Eclipse 23 클래스 생성자
https://developernew.tistory.com/158

JAVA Eclipse 24 상속과 다형성 - 상속/메서드 오버라이딩
https://developernew.tistory.com/159

JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습
https://developernew.tistory.com/160

 

JAVA Eclipse 26 추상메서드와 추상클래스
https://developernew.tistory.com/161