2015-02-01 3 views
1

추상 인터페이스 인 NamedAccount.java를 구현하는 BankAccount.java가 있는데, BankAccount.java가 추상화되지 않아 무시할 수 없다는 오류가 계속 발생합니다. 이 문제를 어떻게 수정합니까? 두 상황에서 @Override를 추가하려고 시도했지만 작동하지 않습니다!자바 오버라이드 추상 인터페이스 메서드

BankAccount.java :

public class BankAccount implements NamedAccount { 

    private String myCustomer; 
    private double myBalance; 
    private double myInterest; 
    protected int myMonthlyWithdrawCount; 
    protected double myMonthlyServiceCharges; 

    public BankAccount(final String theNameOfOwner, 
         final double theInterestRate) { 
     myCustomer = theNameOfOwner; 
     myInterest = theInterestRate; 
     myBalance = 0.0; 
     myMonthlyWithdrawCount = 0; 
     myMonthlyServiceCharges = 0; 
    } 

    public double getBalance() { 
     return myBalance; 
    } 

    public boolean processDeposit(final double theAmount) { 
     boolean trueDeposit = false; 
     if (theAmount > 0) { 
     myBalance += theAmount; 
     trueDeposit = true; 
     } 
     return trueDeposit;  
    } 

    public boolean processWithdrawal(final double theAmount) { 
     boolean trueWithdrawal = false; 
     if (theAmount > 0 && theAmount > myBalance) { 
     myBalance -= theAmount; 
     trueWithdrawal = true; 
     } 
     return trueWithdrawal; 
    } 

    public double calculateInterest() { 
     return myBalance * (myInterest/12.0); 
    } 

    public void performMonthlyProcess() { 
     myBalance -= myMonthlyServiceCharges; 
     myBalance += calculateInterest(); 
     myMonthlyWithdrawCount = 0; 
     myMonthlyServiceCharges = 0.0; 
     if (myBalance < 0.0) { 
     myBalance = 0.0; 
     } 
    } 
} 

NamedAccount.java :

public interface NamedAccount { 
    String getAccountHolderName(); 
    void setAccountHolderName(final String theNewName); 
} 

하고이 SafeDepositBoxAccount.java해야 실행할 수 있도록 : 클래스 때문에

public class SafeDepositBoxAccount implements NamedAccount { 


     private String mySafeName; 

     public SafeDepositBoxAccount(final String theNameOfHolder) { 
      mySafeName = theNameOfHolder; 
     } 

     public String getAccountHolderName() { 
      return mySafeName; 
     } 

     public void setAccountHolderName(final String theNewName) { 
      mySafeName = theNewName; 
     } 
    } 
+1

오류의 정확한 텍스트를 공유 할 수 있습니까? – Mureinik

+0

인터페이스 구현은 인터페이스의 모든 메소드를 오버라이드/구현 (다른 단어, 동일한 것)해야 함을 의미합니다.'BankAccount' *는 이러한 메소드를 구현하지 않습니다. – immibis

+0

"BankAccount.java가 추상이 아니기 때문에 재정의 할 수 없다는 오류가 계속 발생합니다." 아닙니다. * 실제 * 오류 메시지를 게시한다고 가정합니다. 그것의 일부 맹 글링 된 버전이 아닙니다. – EJP

답변

2

오류가 당신을 말하고 당신 해야 재정의 클래스가 abstract이 아니기 때문에 (인터페이스의) 메소드. 클래스를 추상화하면 메서드를 재정의 할 필요가 없습니다. 뭔가 같은 코드에서

private String accountHolderName; 
@Override 
public String getAccountHolderName() { 
    return accountHolderName; 
} 
@Override 
public void setAccountHolderName(final String theNewName) { 
    this.accountHolderName = theNewName; 
} 
1

BankAccountNamedAccount을 구현한다고 말하면, menthod의 def 은행 계좌 클래스에 here이라고 표시된및 getAccountHolderName과 같이 계약서에 명시된대로 처리해야합니다. 그렇지 않으면 컴파일러에서 클래스를 추상 클래스로 정의해야하므로 다른 클래스가 추상 클래스를 확장하고 구체적인 클래스를 형성하는 두 가지 메소드를 정의 할 수 있습니다. 당신이 컴파일 오류 없애려면

는, 당신은 당신이 SafeDepositBoxAccount 클래스에 정의 된 것처럼 그 두 가지 방법을 재정의해야 :

public class BankAccount implements NamedAccount { 
    ..... 
    private String accountHolderName; 
    @Override 
    public String getAccountHolderName() { 
     return accountHolderName; 
    } 
    @Override 
    public void setAccountHolderName(final String theNewName) { 
     this.accountHolderName = theNewName; 
    } 
} 
-2
public abstract class BankAccount implements NamedAccount { 

    abstract String getAccountHolderName(); 

    abstract void setAccountHolderName(final String theNewName); 

} 
1

는 NamedAccount는 경우를 제외하고는 인터페이스의 모든 메소드를 구현해야합니다 (는 BankAccount이 경우 SafeDepositBoxAccount) 인터페이스를 구현하는 인터페이스 및 클래스 클래스는 추상화으로 선언됩니다.

는 BankAccount 클래스의 경우, getAccountHolderName의 구현이없고 setAccountHolderName이 제공되지 않으며, NamedAccount 클래스는 추상적로 표시되었습니다 둘. 따라서이 오류가 발생했습니다. 모두 getAccountHolderName에 대한

  1. 어느 추상적으로는 BankAccount 클래스를 선언,
  2. 또는 쓰기 구현 및 setAccountHolderName 방법 :

    는 두 가지 해결책이 있습니다.

참고 : @Override는 메서드 선언이 수퍼 클래스의 메서드 선언을 재정의하려는 경우 사용됩니다. BankAccount의 모든 메소드에는 수퍼 클래스에 해당 메소드가 없으므로 @Override를 사용할 수 없습니다.

관련 문제