|
|
Question: how do these access specifiers apply to access by methods inside a derived class ???
|
|
In other words:
|
|
public class CheckingAccount extends BankAccount { // Inherits: // Variables: accNum, name, balance // Methods: deposit(amount), withdraw(amount) /* ---------------------------------------------- Constructor: CheckingAccount(a, n, amount) ---------------------------------------------- */ public CheckingAccount(int a, String n, double amount) { accNum = a; // ERROR !! - Cannot access private variables name = n; // in the base class BankAccount ! 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"; } } |
The Java compiler will report the following errors:
>> javac CheckingAccount.java CheckingAccount.java:12: accNum has private access in BankAccount accNum = a; // Cannot access private variables ^ CheckingAccount.java:13: name has private access in BankAccount name = n; // in the base class BankAccount ! ^ CheckingAccount.java:14: balance has private access in BankAccount balance = amount; ^ 3 errors |
|
|
|
public class BankAccount { protected int accNum; // protected access protected String name; // -- accessible inside "BankAccount" protected double balance; // -- AND: inside any derived class ..... /* ============================================== deposit(amount): Add "amount" to balance ============================================== */ public void deposit( double amount ) { balance += amount; } ..... } |
Then we can access the instance variables inside the derived class CheckingAccount:
public class CheckingAccount extends BankAccount { // Inherits: // Variables: accNum, name, balance // Methods: deposit(amount), withdraw(amount) /* ---------------------------------------------- Constructor: CheckingAccount(a, n, amount) ---------------------------------------------- */ public CheckingAccount(int a, String n, double amount) { accNum = a; // Cannot access private variables name = n; // in the base class BankAccount ! 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 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:
|