2013년 6월 27일 목요일

(130627) 4일차 AbstractFinalClassExam.java (abstract 설명, 4일차 6교시 abstract test )

 - abstract
 : 선언부만 있고 구현부가 없다.
 : public abstract void abstractMethod(int i);
 : subClass에서 Overriding을 하게 하려로 존재.
 : 일반적으로 public 으로 선언

 - abstract Class
 : 추상 클래스, abstract 키워드로 선언
 : Abstract Class는 객체가 될 수 없다
 : 다른 class의 Super class가 되기 위해 존재.
 : 일반적인 클래스와 같이, non-abstract method, member variable을 갖을 수 있다.
 : 일반적인 클래스와 달리, abstract method도 갖을 수 있다.
 : Abstract method를 하나라도 갖는 class는 반드시 abstract class로 선언해야 한다.


 - abstract 구현
// 부모 메소드
abstract void speedDown();

// 자식 메소드
@Override
void speedDown(){
speed = 0;
}


 - 소스
//Error 를 수정하고 이유를 주석으로 처리하세요.

final class FinalClassExam{
final int i = -999999;
}
abstract class AbstractClassExam{ // 추상 클래스
abstract String abstractMethodExam(int i,String s); // 추상 메소드 (Override 필요)
int finalMethodExam(int i, int j){
return i+j;
}
}
class Sample01 extends AbstractClassExam{
String abstractMethodExam(int i,String s){
System.out.println("return "+s+i);
return s+i;
}
int finalMethodExam(int i, int j){
return i*j;
}
}
// class Sample02 extends FinalClassExam{} // final 클래스를 상속받을 수 없다.
class Sample03 extends AbstractClassExam{ // 추상클래스를 상속받으면 꼭 메소드를 써야한다.
 // Sample01을 상속받으면 오버라이딩 부분을 일반 메소드로 인식함
void sampleMethod03(){
System.out.println("void sampleMethod03() 호출 됨");
}

@Override // 추상 메소드에 대한 Override
String abstractMethodExam(int i,String s){
return s+i;
}
}

public class AbstractFinalClassExam{
public static void main(String args[]){
// AbstractClassExam ace = new AbstractClassExam(); // 추상클래스는 객체가 될 수 없다.
FinalClassExam fce = new FinalClassExam();
// fce.i = 100000; // final 변수 변경 안됨
Sample01 s01 = new Sample01();
AbstractClassExam aceS01 = new Sample01(); // 식별자가 이상함. 
aceS01.abstractMethodExam(700,"_999"); // 추상클래스에 접근할 수 없다.
}
}