简体   繁体   中英

Subclass not inheriting all methods

I have a class titled "Account" :

import java.util.Date;

public class Account {

    public int id = 0; //Declare default id as 0
    public double balance = 0; //Declare default balance as 0
    public double annualInterestRate = 0; //Declare default annual interest rate as 0
    public Date dateCreated = new Date(); //Declare date

    //No argument constructor for Account
    public Account() {
    id = 0;
    balance = 0.0;
    annualInterestRate = 0.0;
    }   

    //Constructor that accepts ID, Balance, and Annual Interest Rate
    public Account(int newID, double newBalance, double newAnnualInterestRate) {
    id = newID;
    balance = newBalance;
    annualInterestRate = newAnnualInterestRate;
    }  

    //Get ID
    public int getId() {
        return id;
    }

    //Set ID
    public void setId(int id) {
        this.id = id;
    }

    //Get Balance
    public double getBalance() {
        return balance;
    }

    //Set Balance
    public void setBalance(double balance) {
        this.balance = balance;
    }

    //Get Annual Interest Rate
    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    //Set Annual Interest Rate
    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    //Get Date Created
    public Date getDateCreated() {
        return dateCreated;
    }

    //Withdraw method
    double withdraw(double amount) {
    return balance -= amount;
    }

    //Deposit method 
    double deposit(double amount) {
    return balance += amount;
    }

    //Interest rate method
    double getMonthlyInterestRate() {
    return (balance * annualInterestRate) / 12;
   }

} //End Account class 

I've then created two different sub-classes "PreferredCustomer" and "CommercialCustomer". These two classes should inherit all methods (deposit, withdraw, monthly interest rate, and all getters and setters) of the main "Account" class. The only difference with the sub-classes, is that they have a pre-determined interest rate.

public class PreferredCustomer extends Account {

    public double annualInterestRate;

    public PreferredCustomer() {
    }

    public PreferredCustomer(int id, double balance) {
    super();
    this.annualInterestRate = .04;
    }

} //end PreferredCustomer Class

I have a feeling the way I currently have it set up is not accurate. When testing, the withdraw and deposit methods work, but despite entering a starting balance of $20,000, it still sets the starting balance at $0 and does not calculate the interest rate.

I'm testing the class as such:

public class TestingAccountClass {

public static void main(String[] args) {

    //Create accounts
    CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124, 
          20000.00);

   //Invoking deposit method from account class
   myCommercialCustomerAccount.deposit(3000.00);

   //Display account balance, monthly interest, and date created
   System.out.println("\n\n----Commercial Account---");
   System.out.println("Account Created On: "
       + myCommercialCustomerAccount.getDateCreated());
   System.out.printf("Balance: $%.2f", myCommercialCustomerAccount.getBalance());
   System.out.printf("\nMonthly Interest: $%.2f"
       ,myCommercialCustomerAccount.getMonthlyInterestRate());

When testing the class in this way, the deposit method works, but nothing else from the account class (other than withdraw) appears to be working. Any advice would be appreciated. Thank you!

You do:

CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124, 20000.00);

However,

public PreferredCustomer(int id, double balance) {
    super();
    this.annualInterestRate = .04;
}

You do not do anything with the balance!

You could perhaps change it to:

public PreferredCustomer(int id, double balance) {
    super();
    this.balance = balance;
    this.annualInterestRate = .04;
}

But you would be writing to balance twice.

Also, it is a bad idea to have two variables with the same name (base vs child) -> annualInterestRate .

EDIT ------------------------------------------ EDIT

I would recommend something like this:

public Account() {
    this(0, 0d, 0d);
}  

public Account(int id, double balance, double interestRate) {
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = interestRate;
}   

public PreferredCustomer(int id, double balance) {
    super(id, balance, 0.04d);
}

EDIT2 ------------------------------------------ EDIT2

This is wrong. You are doing integer division.

return (balance * annualInterestRate) / 12;

Change to this:

return (balance * annualInterestRate) / 12d;

or this:

return (balance * annualInterestRate) / 12.0;

In PreferredCustomer there is no mechanism for setting the balance; you are ignoring the balance constructor argument. You haven't directly assigned the balance instance variable and you haven't invoked the superclass constructor. So the balance is 0.

In the PreferredCustomer constructor, either call the superclass constructor that sets the balance, or set it there in the constructor itself, or call setBalance .

I think the problem is somewhere here :

public PreferredCustomer(int id, double balance) {
    super();
    this.annualInterestRate = .04;
    }

Shouldn't you put something inside the super() call? (default values)

ALERT!

For starts you NEVER make your non-final member variables public. I nearly just had a heart attack.

when you type

this.foo

The scope continues to climb up the inherentence tree until it finds an accessible member. So this-->super-->super.super-->super.super.super --... etc

I can tell you how to syntactically solve your problem or tell you how to do this much much better.

I choose the later.

declare a

public abstract double getAnnualInterestRate(); 

in your base class

then change your implementation of getMonthlyInterestRate as such (calling this new method)

   //Interest rate method
    double getMonthlyInterestRate() {
    return (balance * getAnnualInterestRate()) / 12;
   }

in your subclass simply implement this abstract method and return your interest rate.

This will allow you to polymorphically vary the rate and make your implementation future proof. Functions may do anything at all to produce their return value where as member variable are just a bit of data and nothing more

And, please, make all your member variable private

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM