인터페이스
- 완벽히 추상화된 설계도 : 모든 설계도가 추상 메서드<-> 추상 클래스 : 기본 메서드와 추상 메서드 둘 다 가능
- 모든 메서드가 public abstract이며 생략 가능
- 모든 멤버변수가 public static final이며 생략 가능->final : 상수 변수
- interface 키워드를 이용하여 선언
- 클래스에서 해당 인터페이스를 implements 키워드를 이용하여 구현
- 객체 생성이 불가능
- 다중 상속 가능 / 다중 구현도 가능 여러개의 interface implements 가능
- 인터페이스를 구현 받는 하위클래스는 추상 메소드를 반드시 오버라이딩해야 한다.(구현하지 않을 경우 abstract 클래스로 표시해야 함)
- 인터페이스 다형성 적용
package test01_interface;
//클래스는 인터페이스를 구현한다.
//인터페이스는 클래스에 의해 구현되어진다.
//클래스가 인터페이스를 구현할 때는 implements 키워드 사용
public class myClass implements myInterface{
//클래스는 구현하는 인터페이스의 모든 추상메서드를 오버라이드해야함.
@Override
public void method1() {
System.out.println("method 1");
}
@Override
public void method2() {
System.out.println("method 2");
}
}
====================================================================
package test01_interface;
//인터페이스 extends를 이용하여 상속 가능(다중 상속 가능, 구현부가 없음)
//인터페이스가 인터페이스를 상속
//인터페이스의 작성은 interface 키워드 사용
public interface myInterface {
//모든 멤버변수는 public static final이며 생략 가능
public static final int MEMBER_1=10;
int MEMBER_2=20;
//모든 메서드는 public abstract이며 생략 가능
public abstract void method1();
void method2();
}
====================================================================
package test01_interface;
public class test {
public static void main(String[] args) {
myClass m=new myClass();
m.method1();
m.method2();
System.out.println(m.MEMBER_1);
System.out.println(m.MEMBER_2);
}
}
package test02;
public interface HDMI_Input {
void setOutput(HDMI_Output device);
//HDMI_Output device
//device가 HDMI_Output이라는 인터페이스를 구현한 클래스의 객체
void show();
}
====================================================================
package test02;
public interface HDMI_Output {
void output();
}
====================================================================
package test02;
public class computer implements HDMI_Input{
//컴퓨터는 출력 hdmi 출력 장치에 의존
private HDMI_Output outputdevice;
@Override
public void setOutput(HDMI_Output device) {
outputdevice=device;
}
@Override
public void show() {
System.out.print("컴퓨터 화면을 ");
outputdevice.output();
}
}
====================================================================
package test02;
public class monitor implements HDMI_Output{
@Override
public void output() {
// TODO Auto-generated method stub
System.out.println("모니터 화면에 출력합니다.");
}
}
====================================================================
package test02;
public class tv implements HDMI_Output{
@Override
public void output() {
System.out.println("TV화면을 출력합니다.");
}
}
====================================================================
package test02;
public class superComputer implements HDMI_Input{
private HDMI_Output device;
@Override
public void setOutput(HDMI_Output device) {
this.device=device;
}
@Override
public void show() {
System.out.print("슈퍼 컴퓨터의 화면에");
device.output();
}
}
====================================================================
package test02;
public class test {
public static void main(String[] args) {
//출력 장치
//인터페이스는 객체 생성은 불가하지만 해당 인터페이스를 구현한 클래스로 객체 생성 가능
HDMI_Output Monitor=new monitor();//다형성
HDMI_Output TV=new tv();
HDMI_Input Computer=new computer();
// Computer.setOutput(Monitor);
Computer.setOutput(TV);
Computer.show();
HDMI_Input SuperComputer =new superComputer();
SuperComputer.setOutput(Monitor);
SuperComputer.show();
//클래스가 바뀔 때마다 코드를 고치지 않기 위해 인터페이스 사용
//인터페이스를 구현하기만 하면 어떤 클래스의 객체든 사용할 수 있다면 코드를 고칠 필요가 없어진다.
}
}
package test03다중구현;
public interface ableToFly {
void fly();
}
================================================================
package test03다중구현;
public interface ableToHunt {
void hunt();
}
================================================================
package test03다중구현;
public interface ableToSwim {
void swim();
}
================================================================
package test03다중구현;
public class duck implements ableToFly, ableToSwim, ableToHunt{
@Override
public void hunt() {
// TODO Auto-generated method stub
}
@Override
public void swim() {
// TODO Auto-generated method stub
}
@Override
public void fly() {
// TODO Auto-generated method stub
}
}
================================================================
package test03다중구현;
//다중 구현 : ,찍고 나열
public class eagle implements ableToFly,ableToHunt{
@Override
public void fly() {
}
@Override
public void hunt() {
}
}
================================================================
package test03다중구현;
public class test {
public static void main(String[] args) {
duck d=new duck();
eagle e=new eagle();
//d. ->fly, swim, hunt 다 보임
//다형성
ableToFly f=d;
//f. fly밖에 안 보임
}
}
package test04다중상속;
public interface ableToFly {
void fly();
}
========================================================\
package test04다중상속;
public interface ableToHunt {
void hunt();
}
========================================================
package test04다중상속;
public interface ableToSwim {
void swim();
}
========================================================
package test04다중상속;
import test03다중구현.ableToSwim;
//다중 상속 extends 뒤에 ,찍고 나열
public interface duckInterface extends ableToFly, ableToHunt, ableToSwim{
void playInnocent();
}
===========================================================
package test04다중상속;
public class duck implements duckInterface{
@Override
public void hunt() {
// TODO Auto-generated method stub
}
@Override
public void swim() {
// TODO Auto-generated method stub
}
@Override
public void fly() {
// TODO Auto-generated method stub
}
@Override
public void playInnocent() {
System.out.println("오리발을 내밀어요.");
}
}
===========================================================
package test04다중상속;
//다중 구현 : ,찍고 나열
public class eagle implements ableToFly,ableToHunt{
@Override
public void fly() {
}
@Override
public void hunt() {
}
}
===========================================================
package test04다중상속;
public class uglyDuckling implements duckInterface {
@Override
public void swim() {
}
@Override
public void fly() {
// TODO Auto-generated method stub
}
@Override
public void hunt() {
// TODO Auto-generated method stub
}
@Override
public void playInnocent() {
System.out.println("저는 오리가 아닐걸요?");
}
}
===========================================================
package test04다중상속;
public class 도날드덕 implements duckInterface{
@Override
public void fly() {
// TODO Auto-generated method stub
}
@Override
public void hunt() {
// TODO Auto-generated method stub
}
@Override
public void swim() {
// TODO Auto-generated method stub
}
@Override
public void playInnocent() {
System.out.println("저는 진짜 오리가 아니에요");
}
}
===========================================================
package test04다중상속;
public class test {
public static void main(String[] args) {
//다형성
duckInterface d1=new duck();
duckInterface d2=new uglyDuckling();
duckInterface d3=new 도날드덕();
d1.playInnocent();
d2.playInnocent();
d3.playInnocent();
}
}
제네릭
- 타입 파라미터(<T>안에 타입(int, String)을 대입)를 이용하여 클래스, 인터페이스, 메서드를 다양한 타입을 처리할 수 있도록 작성하는 기법
- 미리 사용할 타입을 명시해서 형 변환을 하지 않아도 되게 함.
- 객체의 타입에 대한 안전성 향상 및 형 변환의 번거러움 감소
제네릭 클래스
- 클래스를 정의할 때
- 클래스 안에서 사용되는 자료형을 구체적으로 명시하지 않고 T와 같이 타입 매개변수를 이용하는 클래스