Inheritance
May 15, 2015
Inheritance
Introduction
• In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures such as queues, stacks, collections, windows, menu boxes the structure of different object families can be viewed as a kind of "family" tree.
• Java like most OO languages allows us to use these types of relationships to reuse code and functionality by making classes that use characteristics of "parent" classes
Defining the Differences
• Classes often share capabilities• We want to avoid re-coding these capabilities• Reuse of these would be best to
– Improve maintainability– Reduce cost– Improve “real world” modeling
Inheritance Defined
• When one class re-uses the capabilities defined in another class.
• The new subclass gains all the methods and attributes of the superclass.
Superclass
Subclass
Vehicle
Car Truck
Benefits of Inheritance
• Saves effort of “reinventing the wheel”• Allows us to build on existing code,
specializing without having to copy it, rewrite it, etc.
• To create the subclass, we need to program only the differences between the superclass and the subclass that inherits from it.
• Allows for flexibility in class definitions.
Example
Node
Data
setDatagetData
TreeNode
LeftRight
setLeftsetRightgetLeftgetRight
ListNode
Next
setNextgetNext
Inheritance: Example
class Node {
private int data;
public Node(int newData) {
setData(newData);
}
public void setData(int newData) {
data = newData;
}
public int getData() {
return data;
}
} // Node
Inheritance: Example
class ListNode extends Node {
private ListNode next; // Fields
public ListNode(int newData) { // Constructor
super(newData);
setNext(null);
}
public void setNext(ListNode newNext) {
next = newNext;
}
public ListNode getNext() {
return next;
}
} // ListNode
Inheritance: Example
class TreeNode extends Node {
private TreeNode left;
private TreeNode right;
public TreeNode(int newData) { // Constructor
super(newData);
setLeft(null);
setRight(null);
}
public void setLeft(TreeNode newLeft) {
left = newLeft;
}
public void setRight(TreeNode newRight) {
right = newRight;
}
Inheritance: Example
// class TreeNode (Continued)
public TreeNode getLeft() {
return left;
}
public TreeNode getRight() {
return right;
}
} // TreeNode
Two Types of Inheritance
• Extension– Adds attributes and methods
• Redefinition (Method Overriding)– Changes/modifies existing methods, specializing as
needed
• Consider a primitive bank account which allows only three kinds of transactions:– Deposits– Withdrawals– Ability to check current balance
Inheritance Example: Bank Accounts
The Base Class Bank_Account
Bank_Account
• balance
withdraw
deposit
setBalance
GetBalance
A Superclass Bank_Accountclass BankAccount {
private double balance;
public BankAccount() {setBalance(0);
} // constructor
public void setBalance(double balance) {this.balance = balance;System.out.println(
"New balance now: "+ getBalance());}
public double getBalance() { return balance;
}
A Superclass Bank_Account// class BankAccount continued
public void deposit(double amt) {setBalance(getBalance() + amt);
}
public void withdraw(double amt) {if(getBalance() < amt) {
amt = getBalance(); System.out.println ("Can only withdraw " + amt); }
setBalance(setBalance() - amt);}
} // BankAccount
Inheritance by Extension
• Imagine that we wish to create a new kind of Bank Account that is:– Identical to the base class in all respects, except one
– We want to add the ability for the account to earn interest
• Without inheritance, we’d have to write it from scratch, duplicating code, etc.
• With inheritance, we need code only the new capability and inherit the rest.
Illustration of InheritanceBankAccount
• balance
withdraw
deposit
GetBalance
setBalance
• RATE• MIN_BALANCE
calcInterest
SavingsAccount
Inheritance by Extension
class SavingsAccount extends BankAccount {
public final static double RATE = 0.023;
public final static double MIN_BALANCE = 500.00;
public double calcInterest() {
double interest;
if (getBalance() >= MIN_BALANCE)
interest = getBalance()*RATE;
else
interest = 0.00;
return interest;
}
}
Using Subclasses
class BankExample1 {
public static void main(String args[]) {
SavingsAccount mySavings;
double myInterest;
mySavings = new SavingsAccount();
mySavings.deposit(500.00);
myInterest = mySavings.calcInterest();
mySavings.deposit(myInterest);
mySavings.withdraw(
IOGadget.readDouble(
"Enter amount to withdraw"));
}
}
BankAccount
SavingsAccount
Inheritance by Redefinition
• Imagine that we wish to create a new kind of Savings Account that is identical to Savings Account in all respects, except:– We want to change the way in which withdrawals are handled– The base class already handles withdrawals, but now we want
a subclass that does them differently.
• Without inheritance, we’d have to rewrite it from scratch.
• With inheritance, we need code only the new way that we want withdrawals to work,
Illustration of Redefinition
• RATE• MIN_BALANCE
calcInterest
SavingsAccount
• overdraftOK• OVERDRAFTCHARGE
allowOverdraft
DeluxeSavings
withdraw
Inheritance with Redefinitionclass DeluxeSavings extends SavingsAccount {
private boolean overdraftOK;public final static double OVERDRAFTCHARGE = 20.0;public DeluxeSavings() {
super();setOverdraftOK(false);
}public void setOverdraftOK(boolean odok)
{ overdraftOK = odok; }public void withdraw(double amt) {
if (overdraftOK) { setBalance(getBalance() - amt); if (getBalance() < 0) setBalance(getBalance() - OVERDRAFTCHARGE); } else { super.withdraw(amt); } } // withdraw} // DeluxeSavings
super & this
• super means “look in the superclass”
• Constructor: super();
• Method: super.m();
• Field: super.x;
• this means “look in this class”
• Constructor: this();
• Method: this.m();
• Field: this.x;
Using Subclasses
class BankExample2 {public static void main(String args[]) {
double interest, amt1, amt2;DeluxeSavings mds = new DeluxeSavings();mds.deposit(250.00);amt1 = IOGadget.readDouble("Enter amount to
withdraw");mds.withdraw(amt1);mds.setOverdraftOK(true); interest = mds.calcInterest();mds.deposit(interest);amt2 = IOGadget.readDouble("Enter amount to
withdraw");mds.withdraw(amt2);
} // main} // Demo
Design & Use
• Declare common methods/attributes as high in the class hierarchy as possible
• All subclasses will inherit these capabilities
• Specialize (extend and redefine) in subclasses
• When a method is invoked, the request is serviced by When a method is invoked, the request is serviced by the lowest, most specific class and moves upthe lowest, most specific class and moves upas needed to find a matchas needed to find a match
Method Resolution
DeluxeSavings ds;
ds = new DeluxeSavings();
ds.deposit(1000.00);
ds.deposit(
ds.calcInterest());
ds.withdraw(500.00)
ds.setOverdraftOK(true);
ds.withdraw(2000.00);
DeluxeSavingsoverdraftOK
OVERDRAFT_CHARGEsetOverdraftOK
setOverdraftNOTwithdraw
BankAccountbalancedeposit
withdrawgetBalance
SavingsAccountRATE
MIN_BALANCEcalcInterest
Summary of Inheritance
• Extension take a base class and add new capabilities to it (methods, fields).
• Redefinition (method overriding) takes a base class and redefines an existing method, implementing it in a new way in order to change capability or performance.
• Both allow us to code only the differences.