Inheritance – Programming Activity

After we learned inheritance in class, we were assigned an programming activity which is in 10.4 of the textbook Java Illuminated Fourth Edition.

Generally, this programming activity is to use inheritance to create a subclass of the BankAccount class which we coded earlier. Also, we will create a client of the SavingAccount class which will show how it works.

Exercise

The textbook asks me to copy the source file, and add more to the class according to the instructions.

Exercise 1 & 2 – inheritance/instance variable

Question:

  • indicate that SavingAccount inherits from BankAccount
  • define the private interestRate instance variable (a double, annual rate)
public class SavingsAccount extends BankAccount
{
 public final double DEFAULT_RATE = .03;
 private double interestRate;
...

To inherit from a super class, I need to use the keyword “extends” plus superclass name.

Although now this class is a subclass of BankAccount class, we can add more instance variables, which is called specialisation. Therefore, I directly add a private double variable here. It is private so that its subclass will not be able to directly access it.

Exercise 3 -default constructor

  • write the default constructor (explicitly call the BankAccount Default constructor, set interestRate to default value DEFAULT_RATE, print a message indicating that the constructor is called.)
public SavingsAccount(){
  super();
  //This is explicit, 
  //while implicit means 
  //even if you do not call it, 
  //the default constructor 
  //will be called.
  interestRate = DEFAULT_RATE;
  System.out.println("Default Constructor is called.");
 }

To explicit call the constructor of the super class, BankAccount, I used “super()” which calls the constructor of super class that has not argument. To indicate the constructor is called, I let the system print out a message.

The output will be:

1

This will show up when I create a new object of the subclass using the explicit call of the super class’s default constructor.

Exercise 4 – overloaded constructor

public SavingsAccount(double startBalance, double startInterestRate){
    super(startBalance);
    setInterestRate(startInterestRate);
    System.out.println("Overloaded constructor is called.");
 }

This time I called the overloaded constructor of the super class. This constructor has arguments, so that when you call the super class’s overloaded constructor, you need to include the correct arguments.

2

The output will be like this:

3

The message helps me to make sure which constructor is called.

Exercise 5 – mutator method

This asks me to write a void-return-type method that can implement the interest rate.  It will call the deposit method and pass a month’s worth of interest.

public void applyInterest(){
     deposit(getBalance()*interestRate);
}

Since the interest rate is for a month, I used an accessor method to get the balance and multiplies the monthly interest rate. Then I called the deposit method, which is inherited from the super class.

Exercise 6 – toString method

  • This is returns formatted balance and interestRate.
  • invokes superclass toString to format balance
  • formats interestRate as percent using a NumberFormat object
  • also use the gerPercentInstance method in the NumberFormat class.
public String toString(){
    //return formatted balance and interestRate
    return (super.toString() +
    " Interest rate is " + 
    NumberFormat.getPercentInstance().format(interestRate));
}

In this method I used “super.” notation to call the toString method in the super class. Then I actually override the toString method by adding a formatted interest rate using number format.

The output will be:

4

Conclusion

In this program, I practiced writing a subclass using inheritance. I distinguished between overload and override. I may use what I learned in these exercise into my future programs. I also found out that inheritance is a very efficient way of writing codes because it save codes and make it easier to write similar classes and also to make classes to be specialised.

Leave a comment

Website Built with WordPress.com.

Up ↑