¡ ¡ ¡
Spring ¡2014 ¡
School of Computer Science
Principles of Software Construction: Objects, Design, and - - PowerPoint PPT Presentation
Principles of Software Construction: Objects, Design, and Concurrency An Introduction to Object-oriented Programming, Continued. Modules and Inheritance Spring 2014 Charlie Garrod Christian Kstner School of Computer Science
School of Computer Science
2
3
4
5
6
7
§ Comparing object identity compares references
§ When are two points equal?
8
9
10
11
12
¡
¡
13
14
15
16
17
18
19
20
21
22
«interface» ¡Dog ¡ ¡ getName() ¡: ¡String ¡ getBreed() ¡: ¡String ¡ bark() ¡: ¡String ¡ setName(name ¡: ¡String) ¡ toString() ¡: ¡String ¡ AbstractDog ¡
¡ + ¡getName() ¡: ¡String ¡ + ¡getBreed() ¡: ¡String ¡ + ¡bark() ¡: ¡String ¡ + ¡setName(name ¡: ¡String) ¡ # ¡setBreed(breed ¡: ¡String) ¡ + ¡toString() ¡: ¡String ¡ GermanShephard ¡ ¡ bark() ¡: ¡String ¡ play() ¡
«interface» ¡ brand ¡ Name ¡of ¡class ¡or ¡ interface ¡in ¡top ¡ compartment ¡ Return ¡type ¡ comes ¡aDer ¡ method ¡or ¡field ¡ Methods ¡in ¡ boGom ¡ compartment ¡ Dashed ¡line, ¡open ¡ triangle ¡arrowhead ¡ for ¡implements ¡ Solid ¡line, ¡open ¡ triangle ¡arrowhead ¡ for ¡extends ¡ Italics ¡means ¡ abstract ¡ Italics ¡means ¡ abstract ¡ OpMonal ¡visibility: ¡ + ¡for ¡public ¡
# ¡for ¡protected ¡ ~ ¡for ¡package ¡(not ¡used ¡ much) ¡ Fields ¡in ¡middle ¡ compartment ¡
23
24
25
26
27
«interface» ¡Account ¡
¡
getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ monthlyAdjustment() ¡ «interface» ¡CheckingAccount ¡
¡
getFee() ¡: ¡float ¡ «interface» ¡SavingsAccount ¡
¡
getInterestRate() ¡: ¡float ¡ «interface» ¡InterestCheckingAccount ¡
¡ ¡
CheckingAccountImpl ¡
¡
… ¡ SavingsAccountImpl ¡
¡
… ¡ InterestCheckingAccountImpl ¡
¡
… ¡
28
public abstract class AbstractAccount implements Account { protected float balance = 0.0; public float getBalance() { return balance; } abstract public void monthlyAdjustment(); // other methods… } public class CheckingAccountImpl extends AbstractAcount implements CheckingAccount { public void monthlyAdjustment() { balance -= getFee(); } public float getFee() { /* fee calculation */ } }
«interface» ¡Account ¡
¡
getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ monthlyAdjustment() ¡ CheckingAccountImpl ¡
¡
monthlyAdjustment() ¡ getFee() ¡: ¡float ¡ AbstractAccount ¡
¡
# ¡balance ¡: ¡float ¡
¡+ ¡getBalance() ¡: ¡float ¡ + ¡deposit(amount ¡: ¡float) ¡ + ¡withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ + ¡transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ + ¡monthlyAdjustment() ¡ «interface» ¡CheckingAccount ¡
¡
getFee() ¡: ¡float ¡
29
public abstract class AbstractAccount implements Account { protected float balance = 0.0; public float getBalance() { return balance; } abstract public void monthlyAdjustment(); // other methods… } public class CheckingAccountImpl extends AbstractAcount implements CheckingAccount { public void monthlyAdjustment() { balance -= getFee(); } public float getFee() { /* fee calculation */ } }
«interface» ¡Account ¡
¡
getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ monthlyAdjustment() ¡ CheckingAccountImpl ¡
¡
monthlyAdjustment() ¡ getFee() ¡: ¡float ¡ AbstractAccount ¡
¡
# ¡balance ¡: ¡float ¡
¡+ ¡getBalance() ¡: ¡float ¡ + ¡deposit(amount ¡: ¡float) ¡ + ¡withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ + ¡transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ + ¡monthlyAdjustment() ¡ «interface» ¡CheckingAccount ¡
¡
getFee() ¡: ¡float ¡
protected ¡elements ¡ are ¡visible ¡in ¡ subclasses ¡ an ¡abstract ¡class ¡is ¡ missing ¡the ¡ implementaOon ¡of ¡
¡ an ¡abstract ¡method ¡is ¡leT ¡ to ¡be ¡implemented ¡in ¡a ¡ subclass ¡ no ¡need ¡to ¡ ¡define ¡ getBalance() ¡– ¡the ¡code ¡is ¡ inherited ¡from ¡ AbstractAccount ¡
30
31
«interface» ¡Account ¡
¡
getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ monthlyAdjustment() ¡ «interface» ¡CheckingAccount ¡
¡
getFee() ¡: ¡float ¡ «interface» ¡SavingsAccount ¡
¡
getInterestRate() ¡: ¡float ¡ «interface» ¡InterestCheckingAccount ¡
¡ ¡
32
«interface» ¡Account ¡
¡
getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡ monthlyAdjustment() ¡ «interface» ¡CheckingAccount ¡
¡
getFee() ¡: ¡float ¡ CheckingAccountImpl ¡
¡
monthlyAdjustment() ¡{ ¡… ¡} ¡ getFee() ¡: ¡float ¡{ ¡… ¡} ¡ getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡
¡
BasicAccountImpl ¡
¡
balance ¡: ¡float ¡
¡
getBalance() ¡: ¡float ¡ deposit(amount ¡: ¡float) ¡ withdraw(amount ¡: ¡float) ¡: ¡boolean ¡ transfer(amount ¡: ¡float, ¡ ¡target ¡: ¡Account) ¡: ¡boolean ¡
¡
CheckingAccountImpl ¡ has ¡a ¡BasicAccountImpl ¡
33
34
constructor ¡in ¡this ¡ same ¡class ¡ Invokes ¡a ¡constructor ¡of ¡the ¡
first ¡statement ¡of ¡the ¡ constructor. ¡
35
36
37
38
39