Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance. Introduction In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures.

Similar presentations


Presentation on theme: "Inheritance. Introduction In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures."— Presentation transcript:

1 Inheritance

2 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

3 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

4 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 CarTruck

5 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.

6 Example Node Data setData getData TreeNode Left Right setLeft setRight getLeft getRight ListNode Next setNext getNext

7 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

8 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

9 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; }

10 Inheritance: Example // class TreeNode (Continued) public TreeNode getLeft() { return left; } public TreeNode getRight() { return right; } } // TreeNode

11 Two Types of Inheritance Extension –Adds attributes and methods Redefinition (Method Overriding) –Changes/modifies existing methods, specializing as needed

12 Consider a primitive bank account which allows only three kinds of transactions: – Deposits – Withdrawals – Ability to check current balance Inheritance Example: Bank Accounts

13 The Base Class Bank_Account Bank_Account balance withdraw deposit setBalance GetBalance

14 A Superclass Bank_Account class 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; }

15 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

16 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.

17 Illustration of Inheritance BankAccount balance withdraw deposit GetBalance setBalance RATE MIN_BALANCE calcInterest SavingsAccount

18 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; }

19 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

20 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,

21 Illustration of Redefinition RATE MIN_BALANCE calcInterest SavingsAccount overdraftOK OVERDRAFTCHARGE allowOverdraft DeluxeSavings withdraw

22 Inheritance with Redefinition class 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

23 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;

24 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

25 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 the lowest, most specific class and moves up as needed to find a matchWhen a method is invoked, the request is serviced by the lowest, most specific class and moves up as needed to find a match

26 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); DeluxeSavings overdraftOK OVERDRAFT_CHARGE setOverdraftOK setOverdraftNOT withdraw BankAccount balance deposit withdraw getBalance SavingsAccount RATE MIN_BALANCE calcInterest

27 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.

28


Download ppt "Inheritance. Introduction In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures."

Similar presentations


Ads by Google