주요 이점은 추상 클래스를 인스턴스화 할 수 없다는 것입니다. 당신은 사람들이 문서의 해당 유형에 대한 하위 클래스를 만들고 싶어
public class Document {
public void write(File file) {
// I have no idea how to implement this
}
public void read(File file) {
// I don't know here either
}
}
:이 같은 클래스 Document
있을 수 있습니다
public class MyDocument extends Document {
public void write(File file) {
// Now I know how to implement
}
public void read(File file) {
// this also can be implemented
}
}
을하지만 당신은 사람들이 새로운 Document
의하고 싶지 않은 :
을
:
Document doc = new Document(); // this should not be allowed
는 당신은 사람들이 이런 일을하고 싶지
public abstract class Document
너무 방법이 추상적 만들기 위해 :
public abstract void write(File file);
public abstract void read(File file);
그럼 당신은 Document
을 인스턴스화 할 수 있지만 서브 클래스의 인스턴스 수 (210)
이 솔루션은 Document
추상적를 확인하는 것입니다. 서브 클래스는 read
및 write
을 무시해야합니다.
당신이 묻는 대부분의 내용은 다음에서 설명합니다. http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – kmera