자바 추상 키워드
예시
메서드는 클래스에 속하며 본문이 없습니다 abstract
. abstract
본문은 하위 클래스에서 제공합니다.
// Code from filename: Main.java
// abstract class
abstract class Main {
public String fname = "John";
public int age = 24;
public abstract void study(); // abstract method
}
// Subclass (inherit from Main)
class Student extends Main {
public int graduationYear = 2018;
public void study() { // the body of the abstract method is provided here
System.out.println("Studying all day long");
}
}
// End code from filename: Main.java
// Code from filename: Second.java
class Second {
public static void main(String[] args) {
// create an object of the Student class (which inherits attributes and methods from Main)
Student myObj = new Student();
System.out.println("Name: " + myObj.fname);
System.out.println("Age: " + myObj.age);
System.out.println("Graduation Year: " + myObj.graduationYear);
myObj.study(); // call abstract method
}
}
정의 및 사용
키워드 는 abstract
클래스 및 메서드에 사용되는 비 액세스 한정자입니다.
클래스: 추상 클래스는 개체를 만드는 데 사용할 수 없는 제한된 클래스입니다(이 클래스에 액세스하려면 다른 클래스에서 상속되어야 함).
메서드: 추상 메서드는 추상 클래스에서만 사용할 수 있으며 본문이 없습니다. 본문은 (에서 상속됨) 하위 클래스에 의해 제공됩니다.
관련 페이지
Java Modifiers Tutorial 에서 modifier에 대해 자세히 읽어보십시오 .