© 2006 pearson addison-wesley. all rights reserved 8.1.1 inheritance when one class inherits...
TRANSCRIPT
![Page 1: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/1.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.1
Inheritance
When one class inherits another class, the new class becomes a specialized version of the original.
Java Syntax
public class subclass extends superclass {. . .
}
public class subclass extends superclass {. . .
}
UML (class diagram) Notation
![Page 2: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/2.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.2
public class RedDot extends Oval {
public RedDot( int x, int y, int d ) {super(x, y, d, d);setBackground( Color. red );
}
}
public class RedDot extends Oval {
public RedDot( int x, int y, int d ) {super(x, y, d, d);setBackground( Color. red );
}
}
RedDot dotty;dotty = new RedDot(10, 20, 5);theWindow.add(dotty, 0);dotty.repaint();// methods such as setSize and setLocation// are also available on dotty.
Client code
![Page 3: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/3.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.3
public class BasicCheckbook { protected int balance; // in cents
/** post balance == bd*100 + bc */
public BasicCheckbook(int bd, int bc) { balance = bd*100 + bc; } /** post balance == balance@pre + dd*100 + dc */
public void deposit(int dd, int dc) { balance = balance + dd*100 + dc; } /** post balance == balance@pre - (wd*100 + wc) */ public void withdraw(int wd, int wc) { balance = balance - (wd*100 + wc); }
/* post result == balance */
public int balanceInCents() { return balance; }}
public class BasicCheckbook { protected int balance; // in cents
/** post balance == bd*100 + bc */
public BasicCheckbook(int bd, int bc) { balance = bd*100 + bc; } /** post balance == balance@pre + dd*100 + dc */
public void deposit(int dd, int dc) { balance = balance + dd*100 + dc; } /** post balance == balance@pre - (wd*100 + wc) */ public void withdraw(int wd, int wc) { balance = balance - (wd*100 + wc); }
/* post result == balance */
public int balanceInCents() { return balance; }}
BasicCheckbook
# balance : int
«constructor» + BasicCheckbook( int, int)
«update» + deposit( int, int) + withdraw( int, int)
«query» + balanceInCents() : int
![Page 4: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/4.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.4
public class CheckbookWithStrBalance extends BasicCheckbook {
public CheckbookWithStrBalance(int bd, int bc) { super(bd, bc); }
public String toString() { String dollarStr, centStr; int cents; dollarStr = "" + (balance / 100);
cents = balance % 100; if (cents < 10) { centStr = "0" + cents; } else { centStr = "" + cents; } return "$" + dollarStr + "." + centStr; }}
public class CheckbookWithStrBalance extends BasicCheckbook {
public CheckbookWithStrBalance(int bd, int bc) { super(bd, bc); }
public String toString() { String dollarStr, centStr; int cents; dollarStr = "" + (balance / 100);
cents = balance % 100; if (cents < 10) { centStr = "0" + cents; } else { centStr = "" + cents; } return "$" + dollarStr + "." + centStr; }}
BasicCheckbook
# balance : int
«constructor» + BasicCheckbook( int, int)«update» + deposit( int, int) + withdraw( int, int)«query» + int balanceInCents()
CheckbookWithStrBalance
«constructor» + CheckbookWithStr...( int, int)«query» + String toString()
![Page 5: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/5.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.5
public class CheckbookWithTotals extends CheckbookWithStrBalance { protected int depositTot, withdrawTot;
public CheckbookWithTotals(int bd, int bc) { super(bd, bc); depositTot = 0; withdrawTot = 0; } public void deposit(int dd, int dc) { super.deposit(dd, dc); depositTot = depositTot + dd*100 + dc; } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); withdrawTot = withdrawTot - (wd*100 + wc); } public int deposits() { return depositTot; } public int withdraws() { return withdrawTot; }}
public class CheckbookWithTotals extends CheckbookWithStrBalance { protected int depositTot, withdrawTot;
public CheckbookWithTotals(int bd, int bc) { super(bd, bc); depositTot = 0; withdrawTot = 0; } public void deposit(int dd, int dc) { super.deposit(dd, dc); depositTot = depositTot + dd*100 + dc; } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); withdrawTot = withdrawTot - (wd*100 + wc); } public int deposits() { return depositTot; } public int withdraws() { return withdrawTot; }}
BasicCheckbook # int balance
«constructor» + BasicCheckbook( int, int)«update» + deposit( int, int) + withdraw( int, int)«query» + int balanceInCents()
CheckbookWithStrBalance
«constructor» + CheckbookWith...( int, int)«query» + String toString()
CheckbookWithTotals # int depositTot # int withdrawTot
«constructor» + CheckbookWithTotals( int, int)«update» + deposit( int, int) + withdraw( int, int)«query» + int deposits() + int withdraws()
![Page 6: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/6.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.6
public class CheckbookWithRedInk extends CheckbookWithTotals {
public CheckbookWithRedInk(int bd, int bc) { super(bd, bc); } public void deposit(int dd, int dc) { super.deposit(dd, dc); if (dd*100+dc < 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance - 1000; } } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); if (wd*100+wc > 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance - 1000; } } public String toString() { String str; if (balance >= 0) { str = super.toString(); } else { balance = -balance; str = "(" + super.toString() + ")"; balance = -balance; } return str; }}
public class CheckbookWithRedInk extends CheckbookWithTotals {
public CheckbookWithRedInk(int bd, int bc) { super(bd, bc); } public void deposit(int dd, int dc) { super.deposit(dd, dc); if (dd*100+dc < 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance - 1000; } } public void withdraw(int wd, int wc) { super.withdraw(wd, wc); if (wd*100+wc > 0 && balance < 0) { System.out.println("$10 surcharge"); balance = balance - 1000; } } public String toString() { String str; if (balance >= 0) { str = super.toString(); } else { balance = -balance; str = "(" + super.toString() + ")"; balance = -balance; } return str; }}
BasicCheckbook # int balance
«constructor» + BasicCheckbook( int, int)«update» + deposit( int, int) + withdraw( int, int)«query» + int balanceInCents()
CheckbookWithStrBalance
«constructor» + CheckbookWith...( int, int)«query» + String toString()
CheckbookWithTotals # int depositTot # int withdrawTot
«constructor» + CheckbookWithTotals( int, int)«update» + deposit( int, int) + withdraw( int, int)«query» + int deposits() + int withdraws()
![Page 7: © 2006 Pearson Addison-Wesley. All rights reserved 8.1.1 Inheritance When one class inherits another class, the new class becomes a specialized version](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649d0f5503460f949e5658/html5/thumbnails/7.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 8.1.7
public class Driver{private CheckbookWithRedInk checkbook;
public Driver() {checkbook = new CheckbookWithRedInk( 100, 0 );checkbook.deposit( 20, 0 );checkbook.withdraw( 125, 99 );System.out.println("Final Balance: " + checkbook.toString());
}}
public class Driver{private CheckbookWithRedInk checkbook;
public Driver() {checkbook = new CheckbookWithRedInk( 100, 0 );checkbook.deposit( 20, 0 );checkbook.withdraw( 125, 99 );System.out.println("Final Balance: " + checkbook.toString());
}}
CheckbookWithRedInk # int balance # int depositTot # int withdrawTot
«constructor» + CheckbookWithRedInk( int, int)«update» + deposit( int, int) + withdraw( int, int)«query» + int balanceInCents() + String toString() + int deposits() + int withdraws()
A flattened version of the class
A client