- Interface
인터페이스
1. 인터페이스의 멤버는 상수, 추상메서드, default 메서드, static 메서드만 가능함.
생성자를 만들 수 없다.
2. 인터페이스의 모든 멤버의 접근제한자는 public임
3. 직접 객체화는 불가 => 구현클래스의 객체화를 통해서 객체화 됨.
4. 인터페이스간의 상속은 다중 상속이 가능
클래스간의 상속은 단일 상속임.(나의 부모클래스가 한개면 단일 상속)
5. 클래스와 인터페이스의 관계는 구현(implements)으로 표현함
=> 다중구현이 가능함.
6. 구현클래스의 객체는 구현된 인터페이스의 형으로 형변환이 가능함.
📌
package chap8;
interface Printerable{//Printerable : 상수와 추상메서드로만 이루어졌다
// public static final int INK = 100; //상수
int INK = 100; //public static final 생략됨
// public abstract void print();
void print(); //추상메서드. public abstract 생략됨
}
interface Scannerable{
void scan();
}
interface Faxable{
String FAX_NO = "02-1234-5678";
void send(String no);
void receive(String no);
}
interface Complexerable extends Printerable,Scannerable,Faxable{}
/*
* Complexer : 구현 클래스.
* 추상 메서드 구현해야함.
*/
class Complexer implements Complexerable{
int ink; //일반적인 인스턴스 변수
Complexer(){
this.ink = INK; //printerable.INK 가 맞지만
//Printerable을 상속 받았기 때문에 생략가능
}
@Override
public void print() {
System.out.println("종이에 출력합니다. 남은 잉크량:"+--ink);
}
@Override
public void scan() {
System.out.println("이미지를 스캔하여 파일에 저장합니다.");
}
@Override
public void send(String no) {
System.out.println(FAX_NO+"에서"+no+"번호로 FAX를 전송합니다.");
}
@Override
public void receive(String no) {
System.out.println(no+"에서"+FAX_NO+"로 FAX를 받았습니다.");
}
}
public class InterfaceEx1 {
public static void main(String[] args) {
System.out.println(Printerable.INK);
//오류: Printerable.INK = 200; //상수기 때문에 변경 불가
//오류: Complexerable c1 = new Complexerable(); //인터페이스는 직접 객체화 블가능하기 때문에
Complexer c1 = new Complexer();
System.out.println(c1.ink);//100
c1.print();
System.out.println(c1.ink);//99
c1.scan();
c1.receive("02-5678-1234");
c1.send("02-5678-1234");
System.out.println(c1.INK);//100
if(c1 instanceof Complexerable) {
System.out.println("c1 객체는 Complexerable 객체임");
Complexerable c = c1; //자동 형변환이 됨. 형변환 연산자 생략가능
c.print();
c.scan();
c.receive("02-5678-1234");
c.send("02-5678-1234");
System.out.println(c.INK);
//오류: System.out.println(c.ink); //ink는 Complexerable 인터페이스의 멤버가 아니기때문에
}
if(c1 instanceof Printerable) {
System.out.println("c1 객체는 Printerable 객체임");
Printerable p = c1;
p.print();
//오류: p.scan(); //Scannerable 멤버 임
//오류: p.receive("02-5678-1234"); //Faxable 멤버임
//오류: p.send("02-5678-1234"); //Faxable 멤버임
System.out.println(p.INK);
//오류: System.out.println(p.ink); //ink는 Printerable 인터페이스의 멤버가 아니기때문에
}
if(c1 instanceof Scannerable) {
System.out.println("c1 객체는 Scannerable 객체임");
Scannerable s = c1;
//오류: s.print(); //Printerable 멤버임
s.scan();
//오류: s.receive("02-5678-1234"); //Faxable 멤버임
//오류: s.send("02-5678-1234"); //Faxable 멤버임
//오류: System.out.println(s.INK); //Printerable 멤버임
//오류: System.out.println(s.ink); //ink는 Scannerable 인터페이스의 멤버가 아니기때문에
}
if(c1 instanceof Faxable) {
System.out.println("c1 객체는 Faxable 객체임");
Faxable f = c1;
//오류: f.print(); //Printerable 멤버임
//오류: f.scan(); //Scannerable 멤버 임
f.receive("02-5678-1234");
f.send("02-5678-1234");
//오류: System.out.println(f.INK); //Printerable 멤버임
//오류: System.out.println(f.ink); //ink는 Complexerable 인터페이스의 멤버가 아니기때문에
}
}
}
- Interface (리턴타입이 인터페이스인 경우)
📌
package chap8;
//인터페이스가 리턴 타입으로 사용
class LazerZet implements Printerable{
@Override
public void print() {
System.out.println("레이터 printer로 출력");
}
}
class InkZet implements Printerable{
@Override
public void print() {
System.out.println("잉크젯 printer로 출력");
}
}
class PrinterManager{
public static Printerable getPrinter(String type) {
if(type.equals("INK")) return new InkZet();
else return new LazerZet();
}
}
public class InterfaceEx2 {
public static void main(String[] args) {
Printerable a = PrinterManager.getPrinter("INK");
a.print();
a = PrinterManager.getPrinter("LAZER");
a.print();
}
}
- Interface(인터페이스의 객체화)
인터페이스의 객체화 : 구현클래스로 객채화
인터페이스 타입의 참조변수 = new 인터페이스() { 추상메서드 오버라이딩; }
추상클래스의 객체화 : 하위클래스의 객체로 객체화
추상클래스 변수 = new 추상클래스() { 추상메서드 오버라이딩; }
📌
package chap8;
interface Action {
void action(); // 오버라이딩을 했을때 접근제한자는 public이 되어야 함.
//(넓은 범위로 되어야 하므로)
}
abstract class Abs{
abstract void method(); //접근제한자는 (default)
}
public class InterfaceEx3 {
public static void main(String[] args) {
//일회성 객체로 사용됨. => 이름없는 내부클래스.
Action a = new Action() { //Action 타입으로만 사용 가능
public void action() { // public 을 반드시 붙여줘야함.
// why?오버라이딩을 하기 위하여.
// 클래스에서는 반드시 오버라이딩 할 필요 있음
System.out.println("Action인터페이스의 객체 구현");
}
};
a.action();
Abs ab = new Abs() {
void method() { //오버라이딩시 접근제한자는 같거나 넓은 범위 가능
// public void method() 가능
// protected void method() 가능
System.out.println("Abs 클래스의 객체 구현");
}
};
ab.method();
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
| [Java] chap8 : 인터페이스 - Exam1 (0) | 2023.04.15 |
|---|---|
| [Java] chap8 : 인터페이스 - Interface(복합예제) (0) | 2023.04.15 |
| [Java] chap7 : 클래스의 관계 - Final 제한자, Final 메서드, Final 클래스 (0) | 2023.04.15 |
| [Java] chap7 : 클래스의 관계 - 다형성 Exam1 (0) | 2023.04.15 |
| [Java] chap7 : 클래스의 관계 - 상속2 (0) | 2023.04.15 |