public class BankAccount { private int accNum; // Private access private String name; // -- accessible ONLY inside private double balance; // -- the class "BankAccount" /* ==================================================== Constructor 1: initialize all 3 instance variables ==================================================== */ public BankAccount(int a, String n, double amount) { accNum = a; name = n; balance = amount; } .... } |
public class CheckingAccount extends BankAccount
{
// Inherits:
// Variables: accNum, name, balance (private !!!)
// Methods: deposit(amount), withdraw(amount)
/* ----------------------------------------------
Constructor: CheckingAccount(a, n, amount)
---------------------------------------------- */
public CheckingAccount(int a, String n, double amount)
{
accNum = a; // Error !
name = n;
balance = amount;
}
/* -------------------------------------
transfer(toAccount, amount)
------------------------------------- */
public void transfer( BankAccount toAccount, double amount )
{
this.withdraw(amount); // deduct amount from this account;
toAccount.deposit(amount); // add amount to "toAccount";
}
}
|
|
|
public class CheckingAccount extends BankAccount
{
// Inherits:
// Variables: accNum, name, balance (private !!!)
// Methods: deposit(amount), withdraw(amount)
/* ----------------------------------------------
Constructor of the derived class
---------------------------------------------- */
public CheckingAccount(int a, String n, double amount)
{
/* --------------------------------------
What we want to do:
Initialize accNum with a
Initialize name with n
Initialize balance with amount
-------------------------------------- */
"BankAccount( a, n, amount );" // Invoke a constructor in BankAccount class !!!
}
/* -------------------------------------
transfer(toAccount, amount)
------------------------------------- */
public void transfer( BankAccount toAccount, double amount )
{
this.withdraw(amount); // deduct amount from this account;
toAccount.deposit(amount); // add amount to "toAccount";
}
}
|
super ( parameters ); |
Notes:
|
Example:
public class CheckingAccount extends BankAccount
{
// Inherits:
// Variables: accNum, name, balance (private !!!)
// Methods: deposit(amount), withdraw(amount)
/* ----------------------------------------------
Constructor of the derived class
---------------------------------------------- */
public CheckingAccount(int a, String n, double amount)
{
/* --------------------------------------
What we want to do:
Initialize accNum with a
Initialize name with n
Initialize balance with amount
-------------------------------------- */
super( a, n, amount ); // Invoke a constructor in BankAccount class !!!
}
/* -------------------------------------
transfer(toAccount, amount)
------------------------------------- */
public void transfer( BankAccount toAccount, double amount )
{
this.withdraw(amount); // deduct amount from this account;
toAccount.deposit(amount); // add amount to "toAccount";
}
}
|
public class TestChecking { public static void main( String[] args ) { CheckingAccount acc1 = new CheckingAccount(123, "John", 1000.0); CheckingAccount acc2 = new CheckingAccount(789, "Mary", 5000.0); System.out.println( "acc1: " + acc1 ); // Uses toString() inherited System.out.println( "acc2: " + acc2 ); // from BankAccount } } |
Output:
acc1: Account number: 123, Name: John, Balance: 1000.0 acc2: Account number: 789, Name: Mary, Balance: 5000.0 |
How to run the program:
|
Experiment:
|
and run the program. You will see the added message printed out.
This experiment shows you that the constructor CheckingAccount() did indeed invoke the constructor BankAccount() inside the BankAccount class !
|
|
|
How to run the program:
|