09.5.0 java07 inheritance


Published on

Hariprasanna V (9843824677)

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

09.5.0 java07 inheritance

  1. 1. Inheritance
  2. 2. Introduction <ul><li>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 &quot;family&quot; tree. </li></ul><ul><li>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 &quot;parent&quot; classes </li></ul>
  3. 3. Defining the Differences <ul><li>Classes often share capabilities </li></ul><ul><li>We want to avoid re-coding these capabilities </li></ul><ul><li>Reuse of these would be best to </li></ul><ul><ul><li>Improve maintainability </li></ul></ul><ul><ul><li>Reduce cost </li></ul></ul><ul><ul><li>Improve “real world” modeling </li></ul></ul>
  4. 4. Inheritance Defined <ul><li>When one class re-uses the capabilities defined in another class. </li></ul><ul><li>The new subclass gains all the methods and attributes of the superclass. </li></ul>Superclass Subclass Vehicle Car Truck
  5. 5. Benefits of Inheritance <ul><li>Saves effort of “reinventing the wheel” </li></ul><ul><li>Allows us to build on existing code, specializing without having to copy it, rewrite it, etc. </li></ul><ul><li>To create the subclass, we need to program only the differences between the superclass and the subclass that inherits from it. </li></ul><ul><li>Allows for flexibility in class definitions. </li></ul>
  6. 6. Example TreeNode Left Right setLeft setRight getLeft getRight Node Data setData getData ListNode Next setNext getNext
  7. 7. Inheritance: Example <ul><li>class Node { </li></ul><ul><li>private int data; </li></ul><ul><li>public Node(int newData) { </li></ul><ul><li>setData(newData); </li></ul><ul><li>} </li></ul><ul><li>public void setData(int newData) { </li></ul><ul><li>data = newData; </li></ul><ul><li>} </li></ul><ul><li>public int getData() { </li></ul><ul><li>return data; </li></ul><ul><li>} </li></ul><ul><li>} // Node </li></ul>
  8. 8. Inheritance: Example <ul><li>class ListNode extends Node { </li></ul><ul><li>private ListNode next; // Fields </li></ul><ul><li>public ListNode(int newData) { // Constructor </li></ul><ul><li>super (newData); </li></ul><ul><li>setNext(null); </li></ul><ul><li>} </li></ul><ul><li>public void setNext(ListNode newNext) { </li></ul><ul><li>next = newNext; </li></ul><ul><li>} </li></ul><ul><li>public ListNode getNext() { </li></ul><ul><li>return next; </li></ul><ul><li>} </li></ul><ul><li>} // ListNode </li></ul>
  9. 9. Inheritance: Example <ul><li>class TreeNode extends Node { </li></ul><ul><li>private TreeNode left; </li></ul><ul><li>private TreeNode right; </li></ul><ul><li>public TreeNode(int newData) { // Constructor </li></ul><ul><li>super (newData); </li></ul><ul><li>setLeft(null); </li></ul><ul><li>setRight(null); </li></ul><ul><li>} </li></ul><ul><li>public void setLeft(TreeNode newLeft) { </li></ul><ul><li>left = newLeft; </li></ul><ul><li>} </li></ul><ul><li>public void setRight(TreeNode newRight) { </li></ul><ul><li>right = newRight; </li></ul><ul><li>} </li></ul>
  10. 10. Inheritance: Example <ul><li>// class TreeNode (Continued) </li></ul><ul><li>public TreeNode getLeft() { </li></ul><ul><li>return left; </li></ul><ul><li>} </li></ul><ul><li>public TreeNode getRight() { </li></ul><ul><li>return right; </li></ul><ul><li>} </li></ul><ul><li>} // TreeNode </li></ul>
  11. 11. Two Types of Inheritance <ul><li>Extension </li></ul><ul><ul><li>Adds attributes and methods </li></ul></ul><ul><li>Redefinition (Method Overriding) </li></ul><ul><ul><li>Changes/modifies existing methods, specializing as needed </li></ul></ul>
  12. 12. <ul><li>Consider a primitive bank account which allows only three kinds of transactions: </li></ul><ul><ul><li>Deposits </li></ul></ul><ul><ul><li>Withdrawals </li></ul></ul><ul><ul><li>Ability to check current balance </li></ul></ul><ul><li>Inheritance Example: Bank Accounts </li></ul>
  13. 13. The Base Class Bank_Account Bank_Account <ul><li>balance </li></ul>withdraw deposit setBalance GetBalance
  14. 14. A Superclass Bank_Account <ul><li>class BankAccount { </li></ul><ul><li>private double balance; </li></ul><ul><li>public BankAccount() { </li></ul><ul><li>setBalance(0); </li></ul><ul><li>} // constructor </li></ul><ul><li>public void setBalance(double balance) { </li></ul><ul><li>this.balance = balance; </li></ul><ul><li>System.out.println( </li></ul><ul><li>&quot;New balance now: &quot;+ getBalance()); </li></ul><ul><li>} </li></ul><ul><li>public double getBalance() { </li></ul><ul><li>return balance; </li></ul><ul><li>} </li></ul>
  15. 15. A Superclass Bank_Account <ul><li>// class BankAccount continued </li></ul><ul><li>public void deposit(double amt) { </li></ul><ul><li>setBalance(getBalance() + amt); </li></ul><ul><li>} </li></ul><ul><li>public void withdraw(double amt) { </li></ul><ul><li>if(getBalance() < amt) { </li></ul><ul><li>amt = getBalance(); </li></ul><ul><li>System.out.println </li></ul><ul><li>(&quot;Can only withdraw &quot; + amt); </li></ul><ul><li>} </li></ul><ul><li>setBalance(setBalance() - amt); </li></ul><ul><li>} </li></ul><ul><li>} // BankAccount </li></ul>
  16. 16. Inheritance by Extension <ul><li>Imagine that we wish to create a new kind of Bank Account that is: </li></ul><ul><ul><li>Identical to the base class in all respects, except one </li></ul></ul><ul><ul><li>We want to add the ability for the account to earn interest </li></ul></ul><ul><li>Without inheritance, we’d have to write it from scratch, duplicating code, etc. </li></ul><ul><li>With inheritance, we need code only the new capability and inherit the rest. </li></ul>
  17. 17. Illustration of Inheritance BankAccount <ul><li>balance </li></ul>withdraw deposit GetBalance setBalance <ul><li>RATE </li></ul><ul><li>MIN_BALANCE </li></ul>calcInterest SavingsAccount
  18. 18. Inheritance by Extension <ul><li>class SavingsAccount extends BankAccount { </li></ul><ul><li>public final static double RATE = 0.023; </li></ul><ul><li>public final static double MIN_BALANCE = 500.00; </li></ul><ul><li>public double calcInterest() { </li></ul><ul><li>double interest; </li></ul><ul><li>if (getBalance() >= MIN_BALANCE) </li></ul><ul><li>interest = getBalance()*RATE; </li></ul><ul><li>else </li></ul><ul><li>interest = 0.00; </li></ul><ul><li>return interest; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. Using Subclasses <ul><li>class BankExample1 { </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>SavingsAccount mySavings; </li></ul><ul><li>double myInterest; </li></ul><ul><li>mySavings = new SavingsAccount(); </li></ul><ul><li>mySavings.deposit(500.00); </li></ul><ul><li>myInterest = mySavings.calcInterest(); </li></ul><ul><li>mySavings.deposit(myInterest); </li></ul><ul><li>mySavings.withdraw( </li></ul><ul><li> IOGadget.readDouble( </li></ul><ul><li> &quot;Enter amount to withdraw&quot;)); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>BankAccount SavingsAccount
  20. 20. Inheritance by Redefinition <ul><li>Imagine that we wish to create a new kind of Savings Account that is identical to Savings Account in all respects, except: </li></ul><ul><ul><li>We want to change the way in which withdrawals are handled </li></ul></ul><ul><ul><li>The base class already handles withdrawals, but now we want a subclass that does them differently. </li></ul></ul><ul><li>Without inheritance, we’d have to rewrite it from scratch. </li></ul><ul><li>With inheritance, we need code only the new way that we want withdrawals to work, </li></ul>
  21. 21. Illustration of Redefinition <ul><li>overdraftOK </li></ul><ul><li>OVERDRAFTCHARGE </li></ul>allowOverdraft DeluxeSavings withdraw <ul><li>RATE </li></ul><ul><li>MIN_BALANCE </li></ul>calcInterest SavingsAccount
  22. 22. Inheritance with Redefinition <ul><li>class DeluxeSavings extends SavingsAccount { </li></ul><ul><li>private boolean overdraftOK; </li></ul><ul><li>public final static double OVERDRAFTCHARGE = 20.0; </li></ul><ul><li>public DeluxeSavings() { </li></ul><ul><li>super(); </li></ul><ul><li>setOverdraftOK(false); </li></ul><ul><li>} </li></ul><ul><li>public void setOverdraftOK(boolean odok) </li></ul><ul><li>{ overdraftOK = odok; } </li></ul><ul><li>public void withdraw(double amt) { </li></ul><ul><li>if (overdraftOK) { </li></ul><ul><li>setBalance(getBalance() - amt); </li></ul><ul><li>if (getBalance() < 0) </li></ul><ul><li>setBalance(getBalance() - OVERDRAFTCHARGE); </li></ul><ul><li>} else { </li></ul><ul><li>super.withdraw(amt); </li></ul><ul><li>} </li></ul><ul><li>} // withdraw </li></ul><ul><li>} // DeluxeSavings </li></ul>
  23. 23. super & this <ul><li>super means “look in the superclass” </li></ul><ul><li>Constructor: super(); </li></ul><ul><li>Method: super.m(); </li></ul><ul><li>Field: super.x; </li></ul><ul><li>this means “look in this class” </li></ul><ul><li>Constructor: this(); </li></ul><ul><li>Method: this.m(); </li></ul><ul><li>Field: this.x; </li></ul>
  24. 24. Using Subclasses <ul><li>class BankExample2 { </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>double interest, amt1, amt2; </li></ul><ul><li>DeluxeSavings mds = new DeluxeSavings(); </li></ul><ul><li>mds.deposit(250.00); </li></ul><ul><li>amt1 = IOGadget.readDouble(&quot;Enter amount to withdraw&quot;); </li></ul><ul><li>mds.withdraw(amt1); </li></ul><ul><li>mds.setOverdraftOK(true); </li></ul><ul><li>interest = mds.calcInterest(); </li></ul><ul><li>mds.deposit(interest); </li></ul><ul><li>amt2 = IOGadget.readDouble(&quot;Enter amount to withdraw&quot;); </li></ul><ul><li>mds.withdraw(amt2); </li></ul><ul><li>} // main </li></ul><ul><li>} // Demo </li></ul>
  25. 25. Design & Use <ul><li>Declare common methods/attributes as high in the class hierarchy as possible </li></ul><ul><li>All subclasses will inherit these capabilities </li></ul><ul><li>Specialize (extend and redefine) in subclasses </li></ul><ul><li>When a method is invoked, the request is serviced by the lowest, most specific class and moves up as needed to find a match </li></ul>
  26. 26. Method Resolution <ul><li>DeluxeSavings ds; </li></ul><ul><li>ds = new DeluxeSavings(); </li></ul><ul><li>ds.deposit(1000.00); </li></ul><ul><li>ds.deposit( </li></ul><ul><li>ds.calcInterest()); </li></ul><ul><li>ds.withdraw(500.00) </li></ul><ul><li>ds.setOverdraftOK(true); </li></ul><ul><li>ds.withdraw(2000.00); </li></ul>DeluxeSavings overdraftOK OVERDRAFT_CHARGE setOverdraftOK setOverdraftNOT withdraw BankAccount balance deposit withdraw getBalance SavingsAccount RATE MIN_BALANCE calcInterest
  27. 27. Summary of Inheritance <ul><li>Extension take a base class and add new capabilities to it (methods, fields). </li></ul><ul><li>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. </li></ul><ul><li>Both allow us to code only the differences. </li></ul>