CSC 7322 : Object Oriented Development J Paul Gibson, A207 [email protected]
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
-
date post
20-Dec-2015 -
Category
Documents
-
view
224 -
download
0
Transcript of Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
History: Harvard Mark I History: Harvard Mark I
1944 - The first large scale, automatic, general purpose, electromechanical calculator (aka IBM Automatic Sequence Control Calculator [ASCC])
Intended to compute navigation tables -- the same purpose as intended by Babbage for the Difference Engine
History: The first bugHistory: The first bug
1945 - Grace Murray Hopper, working in a temporary World War I building at Harvard University on the Mark II computer, found the first computer bug beaten to death in the jaws of a relay.
Design MethodologyDesign Methodology
1. Problem Definition
2. Requirements Analysis
3. Architecture
4. Construction
5. Testing
6. Future Improvements
HierarchiesHierarchies
Humans have found that organizing concepts into hierarchies a useful method of organizing information
Inheritance HierarchiesInheritance Hierarchies
Object oriented languages, such as JAVA allows us to group classes into inheritance hierarchies.
The most general classes are near the rootThe more specific classes are near the
leaves
Example: Banking SystemsExample: Banking Systems
Consider a system that supports Savings and Checking accounts– What are the similarities?– What are the specifics
AccountsAccounts
Both savings and Checking accounts support the idea of– Balance– Deposit– Withdraw
Savings accounts pay interest checking accounts do not
Checking accounts have transaction fees, savings accounts do not
Super & Sub classesSuper & Sub classesMore general concepts are in super classesMore specific concepts are in sub classesSub classes extend (inherit from)
superclassesWhen we find, though CRC analysis, that
two classes share many responsibilities, we think about making a super-class
Why inheritance?Why inheritance?
The power of inheritance is that sub-classes inherit the capabilities of the super-classes they extend
This reduces code redundancy
Example: Banking systemsExample: Banking systems
Bank accounts are a type of object Savings accounts are a type of bank account Checking Accounts are bank accounts
public class BankAccount { . . . }public class SavingsAccount extends BankAccount { . . .}public class CheckingAccount extends BankAccount { . . .}
Bank AccountsBank Accounts
public class BankAccount {private double balance;public double getBalance() { . . . }public void deposit(double d) { . . .}public void withdraw(double d){ . . . }
}
Savings accountSavings account
Savings accounts are like Bank accounts but they support the notion of interest
public class SavingsAccount extends public class SavingsAccount extends BankAccount {BankAccount {
private double interestRate; public void addInterest() {
double interest = getBalance() * interestRate/100;
balance += interest; // will this work?}public SavingsAccount(double rate){
interestRate = rate;}}
Savings AccountsSavings Accountspublic class SavingsAccount extends BankAccount {
private double interestRate; public void addInterest() {
double interest = getBalance() * interestRate/100;
deposit(interest); // where is this defined?}public SavingsAccount(double rate){
interestRate = rate;}
}
Checking accountsChecking accounts
For checking accounts, we need to keep track of transactions
So, we need to add the ability to deduct feesHowever, we also need to change deposit
Checking AccountsChecking Accountspublic class CheckingAccount extends BankAccount {
private int transactionCount;
// how about this?
public void deposit (double amount){transactionCount++;deposit(amount);
} ….
Checking AccountsChecking Accountspublic class CheckingAccount extends BankAccount {
private int transactionCount;
// this works better
public void deposit (double amount){transactionCount++;super.deposit(amount);
} ….
Checking AccountsChecking Accounts// and of course
public void withdraw (double amount){transactionCount++;super.withdraw(amount);
} ….
Checking AccountsChecking Accounts// and, in additionpublic void deductFees (){
if (transactionCount > FREE_TRANS) {double fees = TRANS_FEE *
(transactionCount – FREE_TRANS);super.withdraw(fees);
} transactionCount = 0 ;
}private static final int FREE_TRANS = 3;private static final double TRANS_FEE = 2.0 ;
}
PolymorphismPolymorphism
Inheritance is thought of as an “is-a” relationship– A CheckingAccount “is-a” BankAccount– A BankAccount is not a CheckingAccount
Due to this fact, it is possible to use a subclass object in place of a superclass object
Polymorphism ExamplePolymorphism Example
Consider the problem of “transfer”– We want to be able to transfer
Savings -> savings Savings -> checking Checking -> savings Checking -> checking
In short, we want to transfer from one BankAccount, to another
TransferTransfer
A method of BankAccount
public void transfer(BankAccount other, double amount) {
withdraw(amount);other.deposit(amount);
}
Set up some accountsSet up some accounts
CheckingAccount harrysChecking = new CheckingAccount();
harrysChecking.deposit(1000);
SavingsAccount harrysSavings = new SavingsAccount();
harrysSavings.deposit(5000);
Using TransferUsing Transfer
harrysSavings.transfer(harrysChecking,500);
Does this work?transfer is defined on BankAccountstransfer takes a BankAccount as a
parameter
Polymorphism & ArraysPolymorphism & Arrays
BankAccount myBank = new BankAccount[accnts];myBank[0]= new CheckingAccount();myBank[0].deposit(1000);myBank[1]= new SavingsAccount();myBank[1].deposit(1000);
instanceOf instanceOf
for (int i = 0 ; i<myBank.length;i++){ if (myBank[i] instanceOf CheckingAccount)
myBank[i].deductFees(); if (myBank[i] instanceOf SavingsAccount)
myBank[i].addInterest();}
InterfacesInterfaces
Suppose we wanted to be able to sort Savings accounts based on interest rate
We could write our own sort method, but there are lots of good sorts in the JAVA lib
If objects are of the Comparable type, many methods in the JAVA lib could sort them
One ApproachOne Approach
public class SavingsAccount extends BankAccount, Comparable { . . . .}
This does not work Multiple inheritance ins not support in JAVA
Instead – use an interfaceInstead – use an interface
public interface Comparable { int compareTo(Object other); // no impementation
}
public class SavingsAccount extends BankAccount implements Comparable {
public int compareTo(Object other) {// we supply implementation
}. . . .}
Implement an interfaceImplement an interface
public int compareTo(Object other) { // sub is super, not super sub – So, make it so
SavingsAccount otherAccount = (SavingsAccount) other;
if (interestRate < otherAccount.interestRate) return 1; if (interestRate > otherAccount.interestRate) return -1;}