Upload
jasmin-chapman
View
221
Download
0
Embed Size (px)
DESCRIPTION
Example of Specialization Consider the relationships among classes for the following: TimePiece GrandfatherClock DigitalWatch Note that GrandfatherClock is a TimePiece DigitalWatch is a TimePiece TimePiece GrandfatherClockDigitalWatch The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.
Citation preview
Example of Aggregation
1) aggregation - the class contains other structure(s)
2) specialization - the new class is a special case of the data structure
public class PersonalInfo { private String firstName, lastName; private char midInitial; private Address street; . . .} PersonalInfo
String Address
2
Two ways to use a data structure to implement a class
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Inheritance
When one class inherits another class, the new class becomes a specialized version of the original.
Java Syntax
public class childClass extends parentClass {. . .
}
UML (class diagram) Notation
parentClass
childClass
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Example of Specialization
Consider the relationships among classes for the following:
TimePieceGrandfatherClockDigitalWatch
Note that GrandfatherClock is a TimePieceDigitalWatch is a TimePiece
TimePiece
GrandfatherClock DigitalWatch
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Review of InheritanceHow do you express inheritance in Java?
parentClass
childClass
What is meant by overriding a method? (Note that variables can’t be overridden.)
What does a child class inherit from its parent class?Can the child class declare additional variables & methods?Does the child class inherit constructor methods?
What is the notation for calling a parent class constructor?Parent class constructor calls are only permitted in one location. Where?
How does a child class refer to the parent class version of an overridden method?When is the following expression true?
a instanceof b
Are private members from a parent class accessible within a child class? ... public members? ...protected members? The Object of Data Abstraction
and Structure, David D. Riley© Addison Wesley pub.
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
protected
public private is “-”
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Including a toString method
(returns the balance as a String)
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
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()
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Maintain total amounts for
deposits and withdrawls
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
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()
Permit overdraft (negative balance)
(charge $10 for each transaction in the red)
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
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()
public class Director {private CheckbookWithRedInk checkbook;
public Director() {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
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.