HOWTO · Java
Java 오류 수정Cannot Instantiate the Type
수정 방법은 Java에서 유형 오류를 인스턴스화할 수 없습니다.
이 페이지의 내용
오늘은 Java에서 cannot instanceize type 오류를 수정하는 방법에 대해 알아보겠습니다.
이 유형의 오류는 추상 클래스의 인스턴스를 만들려고 할 때 발생합니다. Java의 추상 클래스에 대해 조금 알아보겠습니다.
Java 오류 수정Cannot Instantiate the Type
우리는 일반적으로 모든 구성 요소 사이에 몇 가지 공통 기능을 제공해야 할 때 추상 클래스를 사용합니다. 클래스를 부분적으로 구현할 수 있습니다.
모든 하위 클래스가 재정의하거나 구현할 수 있는 기능을 생성할 수 있습니다. 그러나 추상 클래스를 인스턴스화할 수는 없습니다.
다음 코드를 보십시오.
abstract class Account { // abstract class Cannot Be initiated...
private int amount;
Account() {
// constructor............
}
public void withDraw(int amount) {
this.amount = this.amount - amount;
}
}
위의 추상 클래스 Account는 인스턴스화할 수 없습니다. 다음 코드를 작성할 수 없음을 의미합니다.
Account acc = new Account(); // Abstract Cannot Intialized......
그래서, 해결책은 무엇입니까? 이 추상 클래스의 구체적인/자식 클래스를 만들고 인스턴스를 만들 수 있습니다.
예를 들어, 너무 많은 유형의 계정이 있습니다. 저축, 사업, 직불카드 등이 될 수 있습니다.
그러나 모두 실계좌이며, 공통점이 있습니다. 이것이 우리가 추상 메서드와 클래스를 사용하는 이유입니다.
다음 코드를 살펴보십시오.
class BusinessAccount extends Account {
private int Bonus;
public void AwardBonus(int amount) {
this.Bonus = Bonus + amount;
}
}
BusinessAccount 클래스는 추상 Account 클래스의 구체적이고 자식 클래스입니다. 이 클래스의 인스턴스를 만들고 작업을 완료할 수 있습니다.
BusinessAccount bb = new BusinessAccount();
// Bussiness Account Can Be intiated Because there is concreate defination..........
따라서 결론은 추상 클래스를 인스턴스화할 수 없다는 것입니다. 대신 해당 자식 클래스를 만들고 동일한 기능에 대해 인스턴스화할 수 있습니다.
다음은 컴퓨터에서 실행할 수 있는 완전한 코드입니다.
abstract class Account { // abstract class Cannot Be intiated...
private int amount;
Account() {
// constructor............
}
public void withDraw(int amount) {
this.amount = this.amount - amount;
}
}
class BusinessAccount extends Account {
private int Bonus;
public void AwardBonus(int amount) {
this.Bonus = Bonus + amount;
}
}
public class Main {
public static void main(String[] args) {
// Account acc = new Account(); // Abstract Cannot Intialized......
BusinessAccount bb = new BusinessAccount();
// Bussiness Account Can Be intiated Because there is concreate defination..........
}
}
추상 클래스에 대해 자세히 알아보려면 여기를 클릭하세요.