Lompang
Lompang

Reputation: 97

Java Override Abstract Interface Method

I have a BankAccount.java that implements NamedAccount.java which is an abstract interface, but it keeps giving me an error that since BankAccount.java is not abstract it can't override it. How do I fix this problem? I tried adding @Override in both situations but it does not work!

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);
 } 

and to make it run you'll need this 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;
       }
     }

Upvotes: 1

Views: 3104

Answers (4)

pradeep
pradeep

Reputation: 1

public abstract class BankAccount implements NamedAccount {

    abstract String getAccountHolderName();

    abstract void setAccountHolderName(final String theNewName);

}

Upvotes: -2

Divya
Divya

Reputation: 53

In your code, NamedAccount is an interface and any class that implements an interface (BankAccount and SafeDepositBoxAccount in this case) MUST implement all of the interface's methods except if the class is declared to be abstract.

For BankAccount class, neither have the implementations of getAccountHolderName and setAccountHolderName been provided, nor has NamedAccount class been marked as abstract. Therefore this error has occurred.

There are two solutions:

  1. Either declare BankAccount class to be abstract,
  2. Or write the implementations for both getAccountHolderName and setAccountHolderName methods.

NOTE: @Override is used when a method declaration is intended to override a method declaration in a superclass. Any method in BankAccount does not have corresponding method in the superclass so you cannot @Override.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201537

The error is telling you that you must override those methods (from your interface) because the class isn't abstract. If you make the class abstract then you wouldn't be required to override the methods. Something like,

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

Upvotes: 2

SMA
SMA

Reputation: 37103

Since your class BankAccount says it implements NamedAccount, you need to implement both the menthod's defined in the contract as mentioned here i.e. setAccountHolderName and getAccountHolderName in your BankAccount class or else as compiler says you need to define your class as an abstract class, so other class can extend your abstract class and define those two method to form a concrete class.

If you want to get rid of the compilation error, then you need to override those two methods just like what you defined in SafeDepositBoxAccount class:

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

Upvotes: 1

Related Questions