articles

Home / DeveloperSection / Articles / Inheritance in Java: Calling the super Constructor

Inheritance in Java: Calling the super Constructor

Jayden Bell2476 16-May-2016

To understand how the super keyword is used for calling the superclass constructors, let’s add the constructors to our class hierarchy here

                  

Asset

-id: int

-type: string

+Asset( )

+Assset( in id: int, in type: string)

+printDescription(): void

                                    

 

BankAccount

-bankName: string

-balance: float

+BankAccount( )

+BankAccount(in bankName: string, in balance: float, in id: int, in type: string)

+printDescription(): void

 

SavingAccount

-interestRate: float

+SavingvAccount( )

+SavingvAccount(in interestRate: float, in bankName: string, in balance: float, in id: int,

  in type:string )

+printDescription(): void









Each class now has two constructors—one with no arguments and the other with a few arguments. Look at the SavingsAccount constructor with five arguments. The SavingsAccount has a total of five fields—one of its own and the other four inherited. Therefore, to fully initialize an object of the SavingsAccount type, we need to accept the values of these five fields from the user. The values for the inherited fields will be passed to the superclass constructor. The superclass

BankAccount constructor takes four arguments—two are used for initializing its own fields and two are passed to the Asset class constructor. Now, let’s look at the program that makes calls to these user-defined superclass constructors.

Super Class- Asset
class Asset {

                     private int id;
                     private String type;
                     public Asset() {
                             System.out.println("Creating Asset ...");
                     }
                     public Asset(int id, String type) {
                             this.id = id;
                             this.type = type;
                     }
                     public void printDescription() {
                             System.out.println("Asset ID: " + id);
                             System.out.println("Asset type: " + type);
                     }

}
Sub Class-Bank Account (Subclass of Asset class)
class BankAccount extends Asset {
                     private String bankName;
                     private int accountNumber;
                     private float balance;
                     public BankAccount() {
                             System.out.println("Creating BankAccount ...");
                     }
                     public BankAccount(String bankName, int accountNumber, float balance,
                                      int id, String type) {
                             super(id, type);
                             this.bankName = bankName;
                             this.accountNumber = accountNumber;
                             this.balance = balance;
                     }
                     public void printDescription() {
                             super.printDescription();
                             System.out.println("Name: " + bankName);
                             System.out.println("Account #: " + accountNumber);
                             System.out.println("Current balance: $" + balance);
                     }

}
SubClass –Saving Account (Subclass of SavingAccount)
class SavingsAccount extends BankAccount {
                     private float interestRate;
                     public SavingsAccount() {
                             System.out.println("Creating SavingsAccount ...");
                     }
                     public SavingsAccount(float interestRate, String bankName,
                                      int accountNumber, float balance, int id, String type) {                              super(bankName, accountNumber, balance, id, type);
                             this.interestRate = interestRate;
                     }
                     public void printDescription() {
                             System.out.println("A savings account");
                             super.printDescription();
                             System.out.println("Interest rate (%): " + interestRate);
                     }

}
Test Class:
public class SuperConstructorApp {
                     public static void main(String[] args) {
                             String lineSeparator = "-------------------";
                             SavingsAccount mindStickSavingsAccount = new SavingsAccount();                              SavingsAccount tomSavingsAccount = new SavingsAccount(4.0f, "AMEX",2015, 500.00f, 2005, "Bank Account");                              System.out.println(lineSeparator);                              System.out.println("MindStick's Savings Account");                              tomSavingsAccount.printDescription();                            System.out.println(lineSeparator);                            System.out.println("Tom's Savings Account");                            jimSavingsAccount.printDescription();                            System.out.println(lineSeparator);
                   }

}
 

·  In SuperConstructorApp class, In the main method, we first construct an object of SavingsAccount class by calling its no-arguments constructor. This creates an uninitialized (or, to be more precise, an initialized object with default values set by the compiler) object of the SavingsAccount class

·   The second statement in the main method then constructs another object of the SavingsAccount type by calling its constructor that takes arguments

·   In SavingAccount classes constructor, The first statement in the constructor is a call to super that takes four arguments. This results in calling the constructor of the BankAccount class, which is the superclass of the SavingsAccount class. The next statement initializes the value of the local field interestRate.

·     In BankAccount classes constructor, The first statement in the constructor body is a call to super that takes two arguments. This results in calling the constructor of the Asset class. The remaining three statements initialize the fields of BankAccount class.

·    In Asset classes constructor, It uses the two arguments to initialize its state,we can see how the user-defined constructors of superclasses are called within the constructor of the current class.

Output:

Creating Asset ...

Creating BankAccount ...

Creating SavingsAccount ...

-------------------

MindStick's Savings Account

A savings account

Asset ID: 0

Asset type: null

Name: null

Account #: 0

Current balance: $0.0

Interest rate (%): 0.0

-------------------

Tom's Savings Account

A savings account

Asset ID: 2005

Asset type: Bank Account

Name: AMEX

Account #: 2015

Current balance: $500.0

Interest rate (%): 4.0

-------------------

Also, If we do not provide an explicit call to super with arguments, the compiler provides a call to super with no arguments, which results in calling a no-arguments constructor of the superclass. If we don’t provide a no-arguments constructor, the compiler makes a call to the default no-arguments constructor, which it provides itself. This happens only if we have not defined any other constructor for the class; otherwise, the compiler throws an error.


Updated 16-Dec-2017

Leave Comment

Comments

Liked By