예. Java로 추상 메소드/클래스를 매우 쉽게 생성할 수 있습니다.
예시
추상 클래스를 만들려면 클래스 선언에서 class 키워드 앞에 abstract 키워드를 사용하면 됩니다.
/* File name : Employee.java */
public abstract class Employee {
private String name; private String address;
private int number; public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address; this.number = number;
}
public double computePay() {
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck() {
System.out.println("Mailing a check to " + this.name + " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
} 추상 메소드를 제외하고 Employee 클래스는 Java의 일반 클래스와 동일하다는 것을 알 수 있습니다. 클래스는 이제 추상적이지만 여전히 3개의 필드, 7개의 메서드 및 1개의 생성자가 있습니다.