[Java] 14. Abstract class & Interface_2.Interface

김미숙's avatar
Feb 18, 2025
[Java] 14. Abstract class & Interface_2.Interface
‼️
서로 다른 장치들이 언결되어서 상호 데이터를 주고받는 규격을 의미한다
상속 관계가 아닌 클래스 간의 유사성을 인코딩하는 데 사용된다
클래스를 정의하는 것과 유사하나 키워드 class 가 아닌 interface를 사용
 
notion image
notion image
notion image
 

기본 인터페이스

package ex07.ch01; /* 삼성 리모컨, 엘지 리모컨 기능: on, off */ // 인터페이스 - 강제화시킴 interface Remocon { // 추상메서드는 무조건 public(접근제어자), abstract가 기본, new 불가능! public abstract void on(); // public, abstract 생략 void off(); } class SamsungRemocon implements Remocon { @Override public void on() { System.out.println("삼성 리모컨 ON"); } @Override public void off() { System.out.println("삼성 리모컨 OFF"); } } class LGRemocon implements Remocon { @Override public void on() { System.out.println("엘지 리모컨 ON"); } @Override public void off() { System.out.println("엘지 리모컨 OFF"); } } public class Inter01 { static void start(Remocon r) { r.on(); } public static void main(String[] args) { Remocon lg = new LGRemocon(); start(lg); } }
 
Share article

parangdajavous