Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter18 : interface 본문

JAVA

chapter18 : interface

GAWON 2023. 5. 16. 18:34
1.1 인터페이스 (interface)
 - 객체의 사용 방법을 정의한 타입으로, 개발 코드와 객체가 서로 통신하는 접점 역할을 한다.
 - 개발 코드가 인터페이스의 메소드를 호출하면 인터페이스는 객체의 메소드를 호출한다.
      그렇기 때문에 개발 코드는 객체의 내부 구조를 알 필요가 없고 인터페이스의 메소드만 알면 된다.
 - 인터페이스는 추상메소드의 모음이다(추상 클래스가 인터페이스로 발전)
 - 특정 규칙을 지킨 추상클래스가 인터페이스이다.
 - 작업지시서 역할을 수행한다.

1.2 인터페이스의 특징		(★추상 클래스와 차이)
 - 상수와 메소드만을 구성 멤버로 갖는다.★
 - 생성자를 가질 수 없다.★
 - 상수는 반드시 선언과 동시에 초기값을 지정해야 한다.
 - 메소드는 실행 블록이 필요없는 추상 메소드로 선언한다.
 - 인터페이스가 되기 위한 규칙
	1) public final static 상수		 		만 선언 할 수 있다. (고정된 값)
	2) public abstract 리턴타입 메소드명() ; 		만 선언 할 수 있다. - 추상 메소드
	3) public default 리턴타입 메소드명(){ }		도 추가 할 수 있다.(단, jdk1.8 이후) - 디폴트 메소드
	4) public static 리턴타입 메소드명(){ }		도 추가 할 수 있다.(단, jdk1.8 이후) - 클래스 메소드
★ - 인터페이스는 상속(extends) 받지 않고, 구현(implements)한다.
★ - 상속도 받고, 구현도 하면 => 다중 상속의 효과를 낼 수 있다.
★ - 여러 인터페이스의 다중 구현이 가능하다.
)예시
	 interface A { }
	 interface B { }
	 interface C implements A, B { }

1.3 인터페이스 구현
 - 객체는 인터페이스에서 정의된 추상메소드와 동일한 이름, 매개타입, 리턴타입을 가진
      실체 메소드를 가지고 있어야 한다.(오버라이딩 해야한다)
Q1. Test01.java
스마트폰 => 전화기 + 컴퓨터

class Phone 				필드 : String owner
							메소드 : Constructor, sendCall(), receiveCall()
interface Computable		메소드 : connectInternet(), playApp()
class SmartPhone 			Phone 상속, Computable 구현
package org.joonzis.test;

class Phone {
	private String owner;

	public Phone(String owner) {
		this.owner = owner;

	}

	public void sendCall() {

		System.out.println(owner + "에게 전화 걸기");

	}

	public void receiveCall() {

		System.out.println(owner + "전화 받기");

	}

}

interface Computable {
	public void connectInternet();

	public void playApp();

}

class SmartPhone extends Phone implements Computable {
	public SmartPhone(String owner) {
		super(owner);

	}

	@Override
	public void connectInternet() {

		System.out.println("인터넷에 연결한다");

	}

	@Override
	public void playApp() {

		System.out.println("앱을 실행한다");

	}

}

public class Test01 {
	public static void main(String[] args) {

		// Phone phone = new SmartPhone("김씨");
		// Computable phone = new SmartPhone("김씨");
		SmartPhone phone = new SmartPhone("김씨");
		phone.sendCall();
		phone.receiveCall();
		phone.connectInternet();
		phone.playApp();

	}
}

Q2. Test02.java
interface Providable		메소드: sightseeing(), leisure(), food()
class KoreaTour				메소드 : Providable 구현을 통해생성
class GuamTour				메소드 : Providable 구현을 통해생성
class TourGuide				필드 : Providable tour
							메소드 : Constructor, sightseeing(), leisure(), food()
★ KoreaTour / GuamTour -  Providable 구현
package org.joonzis.test;

interface Providable {
	public void sightseeing();

	public void leisure();

	public void food();
}

class KoreaTour implements Providable {
	@Override
	public void sightseeing() {
		System.out.println("경복궁");
	}

	@Override
	public void leisure() {
		System.out.println("한복 입기");
	}

	@Override
	public void food() {
		System.out.println("불고기");
	}

}

class GuamTour implements Providable {
	@Override
	public void sightseeing() {
		System.out.println("사랑의 절벽");
	}

	@Override
	public void leisure() {
		System.out.println("난파선 다이빙");
	}

	@Override
	public void food() {
		System.out.println("치킨 칼라구엔");
	}
}

class TourGuide {
	private Providable tour;
	public TourGuide() {}
	public TourGuide(Providable tour) {
		this.tour = tour;
	}

	public void sightseeing() {
		tour.sightseeing();
	}

	public void leisure() {
		tour.leisure();
	}

	public void food() {
		tour.food();
	}
}

public class Test02 {
	public static void main(String[] args) {
		
		TourGuide tour1 = new TourGuide(new KoreaTour());
		
		
	}
}


Q3. Test03.java
파일로 나눠서 실행해보자!!
Animal.java(인터페이스)		메소드 : move()
Dog.java(Animal 구현)
package org.joonzis.test;

public class Test03 {
public static void main(String[] args) {
	Animal dog = new  Dog();
	dog.move();
  }
}
--------------------------------------------
package org.joonzis.test;

public interface Animal {
	public void move();
}

'JAVA' 카테고리의 다른 글

chapter20 : api  (0) 2023.05.16
chapter19 : exception  (0) 2023.05.16
chapter17 : abstract  (0) 2023.05.16
chapter16 : polymorphism  (0) 2023.05.16
chapter15 : access_modifier  (0) 2023.05.16