2022.11.24 JAVA 21일차 인터페이스
2022. 11. 24. 17:13ㆍJAVA
인터페이스
자바에서 다중상속은 지원하지 않으므로, 인터페이스를 통해 다중상속 지원
다른 클래스를 작성할 때 기본이 되는 틀을 제공하면서, 다른클래스 사이의 중간 매개역할까지 담당하는 추상클래스를 의미함.
추상클래스는 추상메소드, 생성자, 일반메소드도 포함
인터페이스는 오로지 추상메소드와 상수만을 포함
인터페이스는 객체화 되지 않는다.
그렇기에 super() 가 필요없다.
문법)
접근제어자 interface 인터페이스이름{
public static final 타입 상수이름 = 값;
...
public abstract 메소드이름(매개변수목록);
...
}
public interface RemoteControl {
public final int MAX_VOLUME = 10; //상수
public final int MIN_VOLUME = 0; //상수
public void turnOn(); //추상 메서드
public void turnOff(); //추상 메서드
public void setVolume(int volume); //추상 메서드
}
- 모든 필드는 public static final
- 모든 메소드는 public abstract
- 제어자는 생략 가능하며 생략된 제어자는 컴파일 시 자동 추가
인터페이스 상속 문법
class 클래스이름 implements 인터페이스이름{ . . . }
예제1) 인터페이스 활용
클래스1(Rgb)
public class Rgb {
private int red; // 빨강 rgb값 설정
private int green; // 초록 rgb값 설정
private int blue; // 파랑 rgb값 설정
public Rgb() //디폴트 생성자
{
this(0,0,0);
}
public Rgb(int red, int green, int blue)
{
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed()
{
return red;
}
public void setRed(int red)
{
this.red = red;
}
public int getGreen()
{
return green;
}
public void setGreen(int green)
{
this.green = green;
}
public int getBlue()
{
return blue;
}
public void setBlue(int blue)
{
this.blue = blue;
}
}
클래스2 (PointColor)
public class PointColor {
//x 좌표값
private int x;
//y 좌표값
private int y;
//RGB 색상
private Rgb rgb;
public PointColor() //디폴트 생성자
{
this(0,0,new Rgb());
}
public PointColor(int x, int y, Rgb rgb)
{
setX(x);
setY(y);
setRgb(rgb);
}
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
public Rgb getRgb()
{
return rgb;
}
public void setRgb(Rgb rgb)
{
this.rgb = rgb;
}
}
인터페이스 (GraphicsCard)
public interface GraphicsCard
{
//제조사
public String company();
//모델
public String model();
//메모리
public int memory();
//출력
public void write(PointColor PointColor);
}
인터페이스 상속 클래스 (AmdRadeon)
public class AmdRadeon implements GraphicsCard
{
private String company; //회사이름
private String model; //제품모델
private int memory; //메모리크기
AmdRadeon(String model, int memory)
{
company = "AMD";
this.model = model;
this.memory = memory;
}
public String company() { //인터페이스 오버라이딩
return company;
}
public String model() { //인터페이스 오버라이딩
return model;
}
public int memory() { //인터페이스 오버라이딩
return memory;
}
public void write(PointColor PointColor) { //인터페이스 오버라이딩
if(PointColor != null) //매개변수 PointColor값이 null인지 확인
{
Rgb rgb = PointColor.getRgb();
System.out.println("---" + company + " GraphicsCard 출력");
System.out.println("1. color를 구성한다.");
if(rgb != null) //rgb값이 null인지 확인
{
System.out.println("Blue : " + PointColor.getRgb().getBlue()); //rgb클래스의 파랑값 출력
System.out.println("Red : " + PointColor.getRgb().getRed()); //rgb클래스의 빨강값 출력
System.out.println("Green : " + PointColor.getRgb().getGreen()); //rgb클래스의 초록값 출력
}
System.out.println("2. 좌표를 구한다.");
System.out.println("x : " + PointColor.getX());
System.out.println("y : " + PointColor.getY());
System.out.println("3.모니터 좌표에 색상출력");
}
}
public String toString()
{
return company()+model()+ "\t" +memory() + "GB";
}
}
메인
public class GraphicsMain {
public static void main(String[] args) {
PointColor point = new PointColor(); //PointColor객체 생성
AmdRadeon amd = new AmdRadeon("960x", 16); //AmdRadeon 객체 생성
System.out.println(amd); //AmdRadeon의 toString메서드 츨력
amd.write(point); //AmdRadeon의 write메서드 실행
System.out.println("\n-------------------------------------------------------------------------\n");
//줄나눔
point.setX(30); //PointColor의 x값 설정
point.setY(90); //PointColor의 y값 설정
point.getRgb().setRed(100); //Rgb의 red값 설정
point.getRgb().setBlue(150); //Rgb의 blue값 설정
point.getRgb().setGreen(200); //Rgb의 Green값 설정
amd.write(point); //AmdRadeon의 write메서드 실행
}
}
출력

'JAVA' 카테고리의 다른 글
| 2022.11.28 JAVA 23일차 예외처리(try ~ catch ~ finally)2 (0) | 2022.11.28 |
|---|---|
| 2022.11.25 JAVA 22일차 예외처리(try ~ catch ~ finally) (0) | 2022.11.25 |
| 2022.11.23 20일차 Vector클래스, 추상화클래스 (0) | 2022.11.23 |
| 2022.11.22 JAVA 19일차 다형성 (0) | 2022.11.22 |
| 2022.11.21 JAVA 18일차 오버라이딩, 접근제어자 (0) | 2022.11.22 |