인터페이스 설계
package chapter09;
public interface InterfaceEx {
// 상수
public int MIN_PRICE = 0;
public int MAX_PRICE = 100000;
// 추상 메서드
public double meanPrice();
public double totalPrice();
// default 메서드
default double getSalePrice(double price) {
return price - (price * 0.05);
}
// static 메서드
static void printPrice(double price) {
System.out.println(price);
}
}
인터페이스 구현
package chapter09;
public class InterfaceExImpl implements InterfaceEx{
@Override
public double meanPrice() {
// TODO Auto-generated method stub
return 0;
}
@Override
public double totalPrice() {
// TODO Auto-generated method stub
return 0;
}
}
테스트
package chapter09;
public class IntereFaceTest {
public static void main(String[] args) {
// static method
InterfaceEx.printPrice(0);
// impl
InterfaceExImpl ie = new InterfaceExImpl();
ie.getSalePrice(0); // default method
ie.meanPrice(); // impl method
ie.totalPrice(); // impl mehod
}
}
Printer Interface
package chapter09;
public interface Printer {
int INK = 100;
void print();
}
Scanner Interface
package chapter09;
public interface Scanner {
void scan();
}
Fax Interfacee
package chapter09;
public interface Fax {
String FAX_NUMBER = "02-1234-5678";
void send(String tel);
void receive(String tel);
}
Interface Implements
package chapter09;
public class Complexer implements Printer, Scanner, Fax {
@Override
public void send(String tel) {
System.out.println(FAX_NUMBER + "에서 "+tel+"로 FAX 전송");
}
@Override
public void receive(String tel) {
System.out.println(tel + "에서 "+FAX_NUMBER+"로 FAX 수신");
}
@Override
public void scan() {
System.out.println("스캔 실행");
}
@Override
public void print() {
System.out.println("출력 실행");
}
}
package chapter09;
public class ComplexerMain2 {
public static void main(String[] args) {
Fax fax = new Fax() {
@Override
public void send(String tel) {
System.out.println("여기는 익명 구현 객체의 send()");
}
@Override
public void receive(String tel) {
System.out.println("여기는 익명 구현 객체의 receive()");
}
};
fax.send("1234");
fax.receive("5678");
}
}
package chapter09;
public interface ComplexcerInterface extends Printer, Scanner, Fax {
}
테스트 - 익명구현
package chapter09;
public class ComplexerMain3 {
public static void main(String[] args) {
ComplexcerInterface ci = new ComplexcerInterface() {
@Override
public void send(String tel) {
System.out.println("여기는 익명 구현 객체의 send()");
}
@Override
public void receive(String tel) {
System.out.println("여기는 익명 구현 객체의 receive()");
}
@Override
public void print() {
System.out.println("여기는 익명 구현 객체의 print()");
}
@Override
public void scan() {
System.out.println("여기는 익명 구현 객체의 scan()");
}
};
ci.send("1234");
ci.receive("5678");
ci.print();
ci.scan();
}
}
Interface
package chapter09;
public interface GraphicCard {
String MEMORY = "2G";
public void process();
}
implements
package chapter09;
public class Amd implements GraphicCard {
public void process() {
System.out.println("AMD 그래픽 처리");
}
}
package chapter09;
public class Nvidia implements GraphicCard {
public void process() {
System.out.println("Nvidia 그래픽 처리");
}
}
test - 인터페이스 다형성
package chapter09;
public class Computer {
public static void main(String[] args) {
GraphicCard gc = new Amd();
System.out.println("메모리 : "+gc.MEMORY);
// Amd로 생성
gc = new Amd(); // 자동 형변환
gc.process();
// Nvidia로 교체
gc = new Nvidia(); // 자동 형변환
gc.process();
}
}
인터페이스 정의
package chapter09;
public interface Animal {
void sleep();
}
인터페이스 구현
package chapter09;
public class Eagle implements Animal {
public void sleep() {
System.out.println("잠을 잔다.");
}
public void eat() {
System.out.println("먹는다.");
}
}
테스트 - 강제 형변환
package chapter09;
public class AnimalMain {
public static void main(String[] args) {
Animal eagle = new Eagle();
eagle.sleep();
// eagle.eat(); // 에러
Eagle eagleObj = (Eagle)eagle; // 강제 형변환
eagleObj.eat(); // Eagle 클래스의 eat() 메서드
System.out.println(eagle instanceof Animal);
}
}
package chapter09;
public interface A {
void a();
}
package chapter09;
public interface AA extends A {
void aa();
}
package chapter09;
public class AAA implements AA {
@Override
public void a() {
}
@Override
public void aa() {
}
}
package chapter09;
public interface AB extends A {
void ab();
}
package chapter09;
public class ABB implements AB {
@Override
public void a() {
}
@Override
public void ab() {
}
}
테스트
package chapter09;
public class InstanceofEx {
public static void main(String[] args) {
A a = new AAA();
AA aa = new AAA();
AAA aaa = new AAA();
A b = new ABB();
AB ab = new ABB();
ABB abb = new ABB();
System.out.println("a > A : " + (a instanceof A));
System.out.println("aa > A : " + (aa instanceof A));
System.out.println("aaa > A : " + (aaa instanceof A));
System.out.println("====================");
System.out.println("b > A : " + (b instanceof A));
System.out.println("ab > A : " + (ab instanceof A));
System.out.println("abb > A : " + (abb instanceof A));
System.out.println("====================");
System.out.println("aaa > AB : " + (aaa instanceof AB));
System.out.println("====================");
System.out.println("aaa > Object : " + (aaa instanceof Object));
System.out.println("abb > Object : " + (abb instanceof Object));
}
}
package chapter09;
public class Parent {
public void method2() {
System.out.println("Parent 클래스의 method2()");
}
}
package chapter09;
public class Child extends Parent {
@Override
public void method22() { // 오류남
super.method2();
}
}