articles

Home / DeveloperSection / Articles / Inheritance in Java: Writing a Multi-level Inheritance Program

Inheritance in Java: Writing a Multi-level Inheritance Program

Allen Scott 7467 16-May-2016

The application discussed in the previous post is based on the asset management classes we have studied so far. We will now look at the concepts of multilevel inheritance with the help of program code. For this purpose, we will use the asset hierarchy

 

Inheritance in Java: Writing a Multi-level Inheritance Program

Super Class: Asset
class Asset
{
          private int id;
          private String type;
          public int getId() {
                   return id;
          }
          public void setId(int id) {
                   this.id = id;
          }
          public String getType() {
                   return type;
          }
          public void setType(String type) {
                   this.type = type;
          }

          public void printDescription() {
                   System.out.println("Asset ID: " + id);
                   System.out.println("Asset type: " + type);
          }

}

 Sub Class Bank Account:

class BankAccount extends Asset {
          private String bankName;
          private int accountNumber;
          private float balance;
          public int getAccountNumber() {
                   return accountNumber;
          }
          public void setAccountNumber(int accountNumber) {
                   this.accountNumber = accountNumber;
          }
          public float getBalance() {
                   return balance;
          }
          public void setBalance(float balance) {
                   this.balance = balance;
          }
          public String getBankName() {
                   return bankName;
          }
          public void setBankName(String bankName) {
                   this.bankName = bankName;
          }
          public void printDescription() {
                   super.printDescription();
                   System.out.println("Name: " + bankName);
                   System.out.printf("Account #: %d%n", accountNumber);
                   System.out.printf("Current balance: $%.02f%n", balance);
          }

}
Sub Class SavingAccount
class SavingsAccount extends BankAccount {
          private float interestRate;
          public void setInterestRate(float interestRate) {
                   this.interestRate = interestRate;
          }

          public void printDescription() {
                   System.out.println("A savings account");
                   super.printDescription();                    System.out.printf("Interest rate (%%): %.02f%n", interestRate);

          }

}
Sub Class CheckingAccount
class CheckingAccount extends BankAccount {
          private float overdraftLimit;
          public void setOverdraftLimit(float overdraftLimit) {
                   this.overdraftLimit = overdraftLimit;
          }

          public void printDescription() {
                   System.out.println("A checking account");
                   super.printDescription();
                   System.out.printf("Overdraft limit: $%.02f%n", overdraftLimit);
          }

}
Test class Asset Mgmt
public class AssetMgmt {

          private SavingsAccount tomSavingsAccount;
          private CheckingAccount iVisionBusinessAccount;
          public static void main(String[] args) {
                   AssetMgmt manager = new AssetMgmt();
                   manager.createAssets();
                   manager.printAllAssets();

          }
          private void createAssets() {
                   mindStickSavingsAccount = new SavingsAccount();
                   mindStickSavingsAccount.setId(1215);
                   mindStickSavingsAccount.setType("Bank Account");
                   mindStickSavingsAccount.setBankName("Citi bank");
                   mindStickSavingsAccount.setAccountNumber(526702);
                   mindStickSavingsAccount.setBalance(15450.00f);
                   mindStickSavingsAccount.setInterestRate(3.0f);
                   mindStickBusinessAccount = new CheckingAccount();
                   mindStickBusinessAccount.setId(1234);
                   mindStickBusinessAccount.setType("Bank Account");
                   mindStickBusinessAccount.setBankName("Bank of America");
                   mindStickBusinessAccount.setAccountNumber(76953);
                   mindStickBusinessAccount.setBalance(678256.00f);
                   mindStickBusinessAccount.setOverdraftLimit(50000.00f);
          }
          private void printAllAssets() {
                   String lineSeparator = "-------------------";
                   System.out.println(lineSeparator);
                   mindStickSavingsAccount.printDescription();
                   System.out.println(lineSeparator);
                   mindStickBusinessAccount.printDescription();
                   System.out.println(lineSeparator);

          }

}
Explanation:

The Asset class declares two fields, which are private to the class definition. To access these variables, we provide the corresponding getter/setter methods. Besides the two fields, we declare a printDescription method in the class definition that prints the values of these fields on the user console.

The BankAccount class inherits the Asset class. The BankAccount class declares three fields; we once again provide the appropriate getter/setter methods for each field and a printDescription method to print the values of these fields on the console. Next, we declare a SavingsAccount class that inherits BankAccount.

The SavingsAccount class, has the unique characteristic of an interest rate that does not apply to a checking account. Therefore, we declare a field to represent this interest rate and a corresponding setter method to set its value. Note that a getter method was not created for this field because we are not going to need it. The printDescription method, like in the earlier cases, prints the value of the class field. Likewise, we create a CheckingAccount class that derives from BankAccount.

The checking account has the unique characteristic of an overdraft limit, which is represented by the overdraftLimit field. Like in earlier cases, we provide the setter method for this field and a printDescription method. Our next task is to write a test application that uses these classes.

Note that this class is declared public, although in reality this is not necessary. We define two variables that hold references to the SavingsAccount and CheckingAccount types.

mindStickSavingsAccount will be used for holding an instance of the SavingsAccount class. As the name suggests, this may be for representing the real-life savings account of a customer named MindStick. Next, we declare another variable of the CheckingAccount type, which may represent a real-life business account of MindStick, Inc.

We first create an instance of the AssetMgmt class. Once an instance is created, we can invoke the instance methods on this object. On the newly created manager object, we invoke the two methods createAssets and printAllAssets. As the names suggest, the first method creates a few assets and the second method lists those objects on the console.

We declare the createAssets method to be private because we know for sure that this method need not be invoked by any code outside the class definition.

Output:
-------------------
A savings account
Asset ID: 1001
Asset type: Bank Account
Name: Citi bank
Account #: 526702
Current balance: $15450.00
Interest rate (%): 3.00
-------------------
A checking account
Asset ID: 1002
Asset type: Bank Account
Name: Bank of America
Account #: 24689
Current balance: $678256.00
Overdraft limit: $50000.00
-------------------

Updated 15-Sep-2020

Leave Comment

Comments

Liked By