Top Banner
Inheritance
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 09.5.0 java07 inheritance

Inheritance

Page 2: 09.5.0 java07 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

Page 3: 09.5.0 java07 inheritance

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

Page 4: 09.5.0 java07 inheritance

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

Page 5: 09.5.0 java07 inheritance

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.

Page 6: 09.5.0 java07 inheritance

Example

Node

Data

setDatagetData

TreeNode

LeftRight

setLeftsetRightgetLeftgetRight

ListNode

Next

setNextgetNext

Page 7: 09.5.0 java07 inheritance

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

Page 8: 09.5.0 java07 inheritance

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

Page 9: 09.5.0 java07 inheritance

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;

}

Page 10: 09.5.0 java07 inheritance

Inheritance: Example

// class TreeNode (Continued)

public TreeNode getLeft() {

return left;

}

public TreeNode getRight() {

return right;

}

} // TreeNode

Page 11: 09.5.0 java07 inheritance

Two Types of Inheritance

• Extension– Adds attributes and methods

• Redefinition (Method Overriding)– Changes/modifies existing methods, specializing as

needed

Page 12: 09.5.0 java07 inheritance

• Consider a primitive bank account which allows only three kinds of transactions:– Deposits– Withdrawals– Ability to check current balance

Inheritance Example: Bank Accounts

Page 13: 09.5.0 java07 inheritance

The Base Class Bank_Account

Bank_Account

• balance

withdraw

deposit

setBalance

GetBalance

Page 14: 09.5.0 java07 inheritance

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;

}

Page 15: 09.5.0 java07 inheritance

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

Page 16: 09.5.0 java07 inheritance

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.

Page 17: 09.5.0 java07 inheritance

Illustration of InheritanceBankAccount

• balance

withdraw

deposit

GetBalance

setBalance

• RATE• MIN_BALANCE

calcInterest

SavingsAccount

Page 18: 09.5.0 java07 inheritance

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;

}

}

Page 19: 09.5.0 java07 inheritance

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

Page 20: 09.5.0 java07 inheritance

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,

Page 21: 09.5.0 java07 inheritance

Illustration of Redefinition

• RATE• MIN_BALANCE

calcInterest

SavingsAccount

• overdraftOK• OVERDRAFTCHARGE

allowOverdraft

DeluxeSavings

withdraw

Page 22: 09.5.0 java07 inheritance

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

Page 23: 09.5.0 java07 inheritance

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;

Page 24: 09.5.0 java07 inheritance

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

Page 25: 09.5.0 java07 inheritance

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

Page 26: 09.5.0 java07 inheritance

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

Page 27: 09.5.0 java07 inheritance

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.

Page 28: 09.5.0 java07 inheritance