CSC 111 - Inheritance Fall 2009
Object Oriented Programming: Inheritance Within object oriented programming, Inheritance is defined as: a mechanism for extending classes by adding variables and methods (functions) Think about the following scenario: Imagine I have a BankAccount class that has as variables double balance, int numberoftransactions, and methods for construction, ti deposit, withdraw, and getbalance. I want a SavingsAccount class, which is essentially a BankAccount, but has an additional variable called interestrate, tr t and a new method called addinterest. t
BankAccount SavingsAccount balance numberoftransactions s construct($) withdraw($) deposit($) getbalance() balance numberoftransactions interestrate construct($) withdraw($) deposit($) getbalance() addinterest() Similar items marked with arrows New items/information circled
I don t want to change BankAccount.java, because it s pretty general and useful - not all accounts need an interest rate. I want to exploit code re-use Don t want to copy all the code from BankAccount.java into SavingsAccount.java, because I m somewhat lazy Any changes to BankAccount activities should affect SavingsAccounts, as SavingsAccounts are just a specialized type of BankAccount If the BankAccount code was just copied into SavingsAccount, then I would have to update both files to ensure that both types of accounts correctly represent the core of how accounts work.
If possible, I would like a simple way to indicate: A particular class (SavingsAccount) inherits all variables and methods from another class (BankAccount), and can add some of its own variables and methods Java allows us to do this but first let s look at more examples
Examples BankAccount BankAccount is the superclass and is general SavingsAccount SavingsAccount is the subclass, inheriting attributes and behaviors from BankAccount, and is more specific/specialized, as it adds it s owns attributes and behaviors
Examples Lion is a subclass of Mammal: A Lion fulfills Mammal properties Live birth Warm-blooded Hair Also has some lion-specific properties Ability to roar Tail Mammal Lion
Examples Can have multiple layers of inheritance: A Lion is a subclass of Cat, which is a subclass of Mammal A Cat fulfills Mammal properties Live birth Warm-blooded Hair A Lion fulfills Cat properties Tail CanPurr Whiskers A Lion has its own properties Large size CanRoar Mammal Cat Lion
Examples Automobile Attributes: year make model transmissiontype i passengercapacity Truck Truck-specific attributes: bedlength
Vehicle Plane Boat Automobile Another example of multi-layer l inheritance it Truck
In a main program, we can call any superclass method on a subclass object: We can call any BankAccount method on a SavingsAccount We can call any SavingsAccount method on a SavingsAccount We can t call a SavingsAccount method on a BankAccount
Superclass BankAccount object constructed handlewithdrawal called Subclass SavingsAccount object constructed handlewithdrawal called Both support getbalance Only SavingsAccount supports addinterest Using subclasses in main looks normal! Blissfully ll unaware are of inheritance it just works.
It is within our definition of a class (datatype), not main, where need to implement inheritance Use normal syntax for indicating new variables and methods New syntax for indicating whom inheriting from New syntax for calling parent s (superclass) methods and for overwriting parent s methods
Syntax: Indicate that your current class extends its superclass At top of file, instead of: public class ClassName use public class ClassName extends AnotherClassName BankAccount - superclass SavingsAccount - subclass
Syntax: Make variables in superclass visible to subclasses Remember, private says only the containing class can see variables protected allows subclasses to also interact with variables Subclasses need to declare only new variables, and they do that normally (private)
A constructor is required to be present for any newly developed subclass, even if it should just do the work of the superclass constructor. Make a call to the superclass constructor by using the syntax super() as the first line of subclass constructor.
New methods can be added to the subclass normally Note old methods don t have to be re- written They are brought in for free from the superclass (parent) class
What if want to overwrite parent s implementation? Just rewrite the method in the subclass In this example, I just added a print statement indicating that I was in the subclass SavingsAccount
What if still need/want to use parent s implementation of a method as part of your own implementation? Use super.method() syntax
Any examples for when would use syntax on last slide combining parents implementation and your own? A savings account that needs to: handle withdrawals by taking money from balance (implemented in superclass), but that also needs to reduce the interest rate when the balance drops below a certain amount (implemented in subclass) A checking account that charges a fee when deposits and withdrawal counts go over a set limit. The deposits and withdrawals are handled d normally, but extra work of charging the fee should be covered in the subclasses.
: Side-by-Side Superclass and Subclass
in BlueJ Large closed triangle arrow indicates inheritance Small open arrow indicates dependency SavingsAccount Inherits from BankAccount BankMain requires SavingsAccount and BankAccount
It is possible to convert variables of: A subclass into a superclass (easy to do) A superclass into a subclass (harder to do) Subclass into a superclass: All SavingsAccounts are BankAccounts, so a BankAccount variable can refer to a SavingsAccount with no problem. SavingsAccount collegefund = new SavingsAccount(10000,0.03); BankAccount anaccount = collegefund;
If a subclass is converted to a superclass and referenced through the superclass, all subclass extras disappear. SavingsAccount collegefund = new SavingsAccount(10000,0.03); BankAccount anaccount = collegefund; anaccount.handledeposit(10000); // legal anaccount.addinterest(); // illegal collegefund.addinterest(); // legal If the collegefund variable is being referenced as a BankAccount, trying to addinterest is illegal (BankAccount doesn t know how to do that).
Idea is similar, in some ways, to converting an int into a double: int myint = 5; double mydouble = myint; // integers are subset of doubles, so easy // to push ints into doubles (without cast) SavingsAccount collegefund = new SavingsAccount(10000,0.03); BankAccount anaccount = collegefund; // SavingsAccounts are subset of BankAccounts, so easy // to push SavingsAccounts into BankAccounts (without // cast) Now that using doubles, can t use integer specific % (remainder) method Now that t using BankAccount, can t use SavingsAccount specific addinterest method