//An instance of this class represents a basic Savings Account // that earns interest in every monthly billing cycle based // on the current balance when the interest is compounded // //Example program to illustrate very basic Java inheritance // Norman Pestaina Fall 2017 // public class SavingsAccount extends BankAccount { //Class Constant: The default rate is also a minimum rate public static final double DEFAULT_INTEREST_RATE = 1.0; //Instance Variable (accountNumber, currentBalance inherited) private double interestRate; //Constructor public SavingsAccount(double initialBalance, double interestRate) { super(initialBalance); //FIRST: Initialize inherited state this.interestRate = interestRate < DEFAULT_INTEREST_RATE ? DEFAULT_INTEREST_RATE : interestRate; } //Constructor public SavingsAccount(double initialBalance) { this(initialBalance, DEFAULT_INTEREST_RATE); } //Constructor public SavingsAccount() { //Parameterless super() IMPLICITLY invoked this.interestRate = DEFAULT_INTEREST_RATE; } //Accessor public double getInterestRate() { return this.interestRate; } //Mutator: specific to SavingsAccount instances public void addInterest() { //MUST use accessor methods to access private inherited state double interest = this.getCurrentBalance() * this.interestRate / 100 / 12; this.deposit(interest); } //Override public String toString() { //EXTEND image to include native state return super.toString() + " Interest Rate " + this.interestRate + "%/Yr."; } //Override public boolean equals(Object other) { //FIRST, compare inherited state if (!super.equals(other)) return false; //Compare native state return this.interestRate == ((SavingsAccount)other).interestRate; } }