Inheritance Part III Lecture Objectives To learn about
Inheritance Part III
Lecture Objectives • To learn about inheritance • To understand how to inherit and override superclass methods • To be able to invoke superclass constructors • To learn about protected and package access control • To understand the common superclass Object and to override its to. String() and equals() methods
Overriding the equals() Method • The equals() method tests for equal contents Figure 4: Two References to Equal Objects
Overriding the equals() Method (Cont’d) • == tests for equal location Figure 5: Two References to the Same Object
Overriding the equals() Method (Cont’d) • Define the equals() method to test whether two objects have equal state • When redefining the equals() method, you cannot change object signature; use a cast instead: public class Coin {. . . public boolean equals(Object other. Object) { Coin other = (Coin) other. Object; return name. equals(other. name) && value == other. value; }. . . }
Overriding the equals() Method (Cont’d) • You should also override the hash. Code() method so that equal objects have the same hash code
Overriding the clone() Method • Copying an object reference gives two references to same object: Bank. Account account 2 = account;
Overriding the clone() Method (Cont’d) • Sometimes, need to make a copy of the object Figure 6: Cloning Objects
Overriding the clone() Method (Cont’d) • Define the clone() method to make new object • Use of the clone() method: Bank. Account cloned. Account = (Bank. Account) account. clone(); • Must cast return value because return type is Object
The Object. clone Method • Creates shallow copies Figure 7: The Object. clone Method Makes a Shallow Copy
The Object. clone Method (Cont’d) • Does not systematically clone all subobjects • Must be used with caution • It is declared as protected; prevents from accidentally calling x. clone() if the class to which x belongs hasn't redefined clone to be public • You should override the clone() method with care
UML Inheritance Diagrams • Typically, a UML class diagram shows only as much as is needed for the design task at hand.
UML Inheritance Diagrams (Cont’d) Figure 8: UML Inheritance Diagram
UML Inheritance Diagrams (Cont’d) • An arrowhead points from a derived class to its base class, indicating an is-a relationship. § For example, a Student is-a Person. • Inherited instance variables and methods can be found by following the arrowheads.
UML Inheritance Diagrams (Cont’d) • More details of the inheritance hierarchy Figure 9: UML Inheritance Diagram of Person and Student Classes
Access Control • Java has four levels of controlling access to fields, methods, and classes: § public access • Can be accessed by methods of all classes § private access • Can be accessed only by the methods of their own class § protected access
Access Control (Cont’d) • Java has four levels of controlling access to fields, methods, and classes: § package access • The default, when no access modifier is given • Can be accessed by all classes in the same package • Good default for classes, but extremely unfortunate for fields
Recommended Access Levels • Instance and static fields: Always private. Exceptions: § public static final constants are useful and safe § Some objects, such as System. out, need to be accessible to all programs (public) § Occasionally, classes in a package must collaborate very closely (give some fields package access); inner classes are usually better
Recommended Access Levels (Cont’d) • Methods: public or private • Classes and interfaces: public or package § Better alternative to package access: inner classes • In general, inner classes should not be public (some exceptions exist, e. g. , Ellipse 2 D. Double) • Beware of accidental package access (forgetting public or private)
Access Modifiers
File Account. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: /** This program tests the Bank. Account class and its subclasses. */ public class Account. Tester { public static void main(String[] args) { Savings. Account moms. Savings = new Savings. Account(0. 5); Checking. Account harrys. Checking = new Checking. Account(100); moms. Savings. deposit(10000);
File Account. Tester. java 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: } moms. Savings. transfer(2000, harrys. Checking); harrys. Checking. withdraw(1500); harrys. Checking. withdraw(80); moms. Savings. transfer(1000, harrys. Checking); harrys. Checking. withdraw(400); // Simulate end of month moms. Savings. add. Interest(); harrys. Checking. deduct. Fees(); System. out. println("Mom's savings balance = $“ + moms. Savings. get. Balance()); System. out. println("Harry's checking balance = $“ + harrys. Checking. get. Balance()); }
File Bank. Account. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class Bank. Account { /** Constructs a bank account with a zero balance. */ public Bank. Account() { balance = 0; } /** Constructs a bank account with a given balance. @param initial. Balance the initial balance */
File Bank. Account. java 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: public Bank. Account(double initial. Balance) { balance = initial. Balance; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw */
File Bank. Account. java 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: public void withdraw(double amount) { balance = balance - amount; } /** Gets the current balance of the bank account. @return the current balance */ public double get. Balance() { return balance; } /** Transfers money from the bank account to another account @param amount the amount to transfer @param other the other account */
File Bank. Account. java 56: 57: 58: 59: 60: 61: 62: 63: } public void transfer(double amount, Bank. Account other) { withdraw(amount); other. deposit(amount); } private double balance;
File Checking. Account. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /** A checking account that charges transaction fees. */ public class Checking. Account extends Bank. Account { /** Constructs a checking account with a given balance. @param initial. Balance the initial balance */ public Checking. Account(double initial. Balance) { // Construct superclass super(initial. Balance); // Initialize transaction count transaction. Count = 0; }
File Checking. Account. java 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: public void deposit(double amount) { transaction. Count++; // Now add amount to balance super. deposit(amount); } public void withdraw(double amount) { transaction. Count++; // Now subtract amount from balance super. withdraw(amount); } /** Deducts the accumulated fees and resets the transaction count. */
File Checking. Account. java 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: } public void deduct. Fees() { if (transaction. Count > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transaction. Count - FREE_TRANSACTIONS); super. withdraw(fees); } transaction. Count = 0; } private int transaction. Count; private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2. 0;
File Savings. Account. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: /** An account that earns interest at a fixed rate. */ public class Savings. Account extends Bank. Account { /** Constructs a bank account with a given interest rate. @param rate the interest rate */ public Savings. Account(double rate) { interest. Rate = rate; } /** Adds the earned interest to the account balance. */
File Savings. Account. java 18: 19: 20: 21: 22: 23: 24: 25: } public void add. Interest() { double interest = get. Balance() * interest. Rate / 100; deposit(interest); } private double interest. Rate;
File Savings. Account. java Output: Mom's savings balance = $7035. 0 Harry's checking balance = $1116. 0
- Slides: 32