5.7 추상 클래스
5.8 인터페이스
public abstract int getValue();//1. 추상 메소드를 포함하는 추상 클래스
abstract class Shape { //추상 클래스 선언
public Shape() {};
public void paint() { draw(); }
abstract public void draw(); //추상 메소드 선언
}
//2. 추상 메소드 없는 추상 클래스
abstract class Person {
String name;
public void load(String name){
this.name = name;
}
}
abstract class Shape { // 추상 클래스 선언
public Shape() {}; // 함수가 어떤 역할을 할지 구현 x
...
}
public class AbstractError {
public static void main(String[] args) }
Shape shape; // 참조 변수 생성은 가능 -> 다른 객체의 참조를 저장할 순 있다.
shape = new Shape(); //컴파일 오류. 추상 클래스는 shape 의 객체를 생성할 수 없다
}
}추상 클래스의 상속 2가지 경우
abstract class Shape { // 추상 클래스
public Shape() {}
public void paint() {draw();}
abstract public void draw(); //추상 메소드
}
abstract class Line extends Shape { //추상 클래스 draw() 를 상속받기 떄문
public String toString() {return "Line";}
//상속 받은 abstract draw() 에 대한 정의가 없다! -> draw() 가 어떻게 수행될지 정보가 없음
}
abstract class Calculator { //추상 클래스 선언
public abstract int add (int a, int b);
public abstract int subtract (int a, int b);
public abstract double average (int [] a);
}
public class GoodCalc extends Calculator {
@Override
public int add(int a, int b) { // add() 추상 메소드 구현
return a+b;
}
@Override
public int subtract(int a, int b) { // subtract() 추상 메소드 구현
return a-b;
}
@Override
public double average(int [] a) { // average() 추상 메소드 구현
double sum = 0;
for (int i=0; i<a.length; i++)
sum += a[i];
return sum/a.length;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator c = new GoodCalc(); //업캐스팅 -> 부모 영역만 접근 가능
//-> 오버라이딩에 의해서 add 함수 호출
//-> (동적 바인딩) 자식 클래스 메소드 호출
//GoodCalc c = new GoodCalc();
System.out.println(c.add(2,3));
System.out.println(c.subtract(2,3));
System.out.println(c.average(new int [] {2,3,4}));
}
}
//result
// 5
// -1
// 3.0
public interface SerialDriver { ... }public abstract 셍략 가능public 만 허용, public static final 생략 가능new PhoneInterface(); -> 오류 PhoneInterface galaxy; -> 참조변수 implementextendsinterface PhoneInterface { //인터페이스 선언
final int TIMEOUT = 10000; //상수 필드 선언
void sendCall(); //추상 메소드 public abstract 가 생략되어있기 떄문에 추상 메소드를 의미
void receiveCall(); //추상 메소드
default void printLogo() { //default 메소드
System.out.println("** Phone **");
}
}
//인터페이스 구현 implements
class SamsungPhone implements PhoneInterface {
// PhoneInterface 의 모든 추상 메소드 구현
@Override
public void sendCall() {
System.out.println("Rrrrrrrr~");
}
@Override
public void receiveCall() {
System.out.println("You get a phone call.");
}
// 추가 메소드 작성
public void flash() {System.out.println("The phone lights up."); }
}
public class InterfaceEx {
public static void main(String[] args) {
// TODO Auto-generated method stub
SamsungPhone phone = new SamsungPhone();
// SamsungPhone phone = new PhoneInterface(); - > flash 떄문에 사용 불가?
// 나랑 관련이 없는 flash 라는 정의가 있기 때문에 오류가 난다 (다시 생각해보기)
phone.printLogo();
phone.sendCall();
phone.receiveCall();
phone.flash();
}
}
//result
// ** Phone **
// Rrrrrrrr~
// You get a phone call.
// The phone lights up.
interface PhoneInterface { //인터페이스 선언
final int TIMEOUT = 10000; //상수 필드 선언
void sendCall(); //추상 메소드
void receiveCall(); //추상 메소드
default void printLogo() { // default 메소드
System.out.println("** Phone **");
}
}
// ---'인터페이스'가 다른 '인터페이스' 상속 ---//
// extends 키워드 이용
interface MobilePhoneInterface extends PhoneInterface {
void sendSMS(); // 새로운 추상 메소드 추가
void receiveSMS(); // 새로운 추상 메소드 추가
}
interface MP3Interface { //인터페이스 선언
public void play();
public void stop();
}
class PDA { //클래스 작성
public int calculate(int x, int y) {
return x + y;
}
}
//SmartPhone 클래스는 PDA 를 상속받고,
//MobilePhoneInterface 와 MP3Interface 인터페이스에 선언된 추상 메소드를 모두 구현한다.
class SmartPhone extends PDA implements MobilePhoneInterface, MP3Interface {
//MobilePhoneInterface의 추상 메소드 구현
@Override
public void sendCall() {
System.out.println("Rrrrrrrr~");
}
@Override
public void receiveCall() {
System.out.println("You get a phone call.");
}
@Override
public void sendSMS() {
System.out.println("Send a cell phone text.");
}
@Override
public void receiveSMS() {
System.out.println("I got a cell phone text.");
}
//MP3Interface의 추상 메소드 구현
@Override
public void play() {
System.out.println("Play music.");
}
@Override
public void stop() {
System.out.println("Stop the music.");
}
// 추가로 작성한 메소드
public void schedule() {
System.out.println("Manage your calendar.");
}
}
public class InterfaceExSnd {
public static void main(String[] args) {
// TODO Auto-generated method stub
SmartPhone phone = new SmartPhone();
phone.printLogo();
phone.sendCall();
phone.play();
System.out.println("If you add 3 and 5 : " + phone.calculate(3,5));
phone.schedule();
}
}
//result
** Phone **
Rrrrrrrr~
Play music.
If you add 3 and 5 : 8
Manage your calendar.
