1. 추상클래스&Interface의 개념
- 추상클래스 : 반드시 오버라이딩해서 사용할 미완성의 메서드 하나이상 가진 미완성 클래스, 객체생성x
- Interface : 추상클래스의 극단으로 모든 메서드가 추상적인 상태
2. 추상클래스 정의와 사용
abstract class Remote{
public int volume = 10;
public int channel = 1;
public void volume_up(){ //완성된 메서드
this.volume++;
}
public void volume_down(){ //완성된 메서드
this.volume--;
}
abstract void channel_change(int i);
}
class TV_Remote extends Remote{ //미완성 클래스는 상속해서 완성시킴
void channel_change(int i){
channel=i;
}
}
public static void main(String[] args){
TV_Remote tv = new TV_Remote();
tv.volume_up(); //11
tv.volume_up(); //12
System.out.printIn("볼륨:" + tv.volume); //12
tv.channel_change(5);
System.out.printIn("채널:" + tv.channel); //5
}
3. Interface 정의와 사용
interface Remote{ //only 생성, 구현x
public void volume_up();
public void volume_down();
public void channel_change(int i);
}
class TV_Remote implemets Remote{ //remote에서 받아서 구현
public int volume = 10;
public int channel = 1;
public void channel_change(int i){
this.channel = i;
}
public void volume_up(){
this.volume++;
}
public void volume_down(){
this.volume--;
}
}
public static void main(String[] args){
TV_Remote tv = new TV_Remote();
tv.volume_up(); //11
tv.volume_up(); //12
System.out.printIn("볼륨:" + tv.volume); //12
tv.channel_change(5);
System.out.printIn("채널:" + tv.channel); //5
}
반응형
'정보처리기사 > [흥달쌤] 실기_JAVA 특강 (완)' 카테고리의 다른 글
JAVA특강 / 06. Static (0) | 2022.04.29 |
---|---|
JAVA특강 / 05. 접근지정자 (0) | 2022.04.29 |
JAVA특강 / 04. 메서드 오버로딩&오버라이딩 (0) | 2022.04.29 |
JAVA특강 / 03. 상속 (0) | 2022.04.29 |
JAVA특강 / 02. 생성자&예외 (0) | 2022.04.28 |