SDSU CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Doc 11, Inheritance & Polymorphism

[To Lecture Notes Index]
San Diego State University -- This page last updated Oct 7, 1996
----------

Contents of Doc 11, Inheritance & Polymorphism

  1. References
  2. Inheritance and Polymorphism
    1. Simplistic Example
    2. Finding Classes
    3. Relationships Between Classes
    4. Banking Classes
    5. Avoid Case (and if) Statements
  3. How to Increase the use of Polymorphism4
  4. Grow Well Shaped & Balanced Trees
  5. Metric Rules of Thumb5

Doc 11, Inheritance & Polymorphism Slide # 1

References


R. J. Abbott, "Programming Design by Informal English Descriptions", Communications of the ACM, Vol. 26, No 11, November 1983, pp. 882 - 894

Grady Booch, Object-Oriented Analysis and Design, 1994

Mark Lorenz, Object-Oriented Software Development: A Practical Guide, 1993, Appendix I Measures and Metrics

Johnson, Ralph E. and Foote, Brain, "Designing Reusable Classes," Journal of Object-Oriented Programming, pages 22-35, June/July 1988.

Shlaer, S. and Mellor, S.J., Object-Oriented Systems Analysis: Modeling the World In Data, Yourdon Press: Prentice-Hall, Englewood Cliffs, New Jersey, 1988.



Doc 11, Inheritance & Polymorphism Slide # 2Listen Here!

Inheritance and Polymorphism

Simplistic Example


Last Federal Virtual Bank wants its entire banking software redone in Java, so it can shut down its branch offices and perform all its business via the WWW.

Customer records include the customers name, address, personal information, passwords, etc. and accounts.

The bank offers various types of accounts: checking, savings, CD, Junior savings accounts.

Bank officers will introduce new types of accounts from time to time.

Checking account charge 12 cents/check unless the average account balance for the month is greater than $1,000. The first three ATM transactions per month are free. Additional ATM transactions cost 50 cents.

All transactions over $1,000 dollars must be reported to the federal government.

Junior savings accounts are for children under the age of 13. Children can deposit any amount into the account. Deposits of over $100 are reported to their parents. Children can withdrawal up to $10 from the account. Individual withdrawals over $10 must be verified by a parent. Total withdrawals over $100 per month are reported to the parents.

Transactions include time & date, amount, location, type of transaction, account number and teller.

Doc 11, Inheritance & Polymorphism Slide # 3Listen Here!

Finding Classes

Abbot[1]

"In an English description of the problem, the nouns are candidates for objects; the verbs are candidates for operations."

Look at these phrases. Some will be obvious classes, some will be obvious nonsense, and some will fall between obvious and nonsense. Skip the nonsense, keep the rest. The goal is a list of candidate objects. Some items in the list will be eliminated, others will be added later. Finding good objects is a skill, like finding a good functional decomposition.
Booch[2]

"The tangible things in problem domain, the roles they play, and the events that may occur form the candidate classes and object of our design."

Disks
Printers Airplanes

Window
File Bank Account


Doc 11, Inheritance & Polymorphism Slide # 4Listen Here!

Categories may become abstract classes
Keep them as individual classes at this point

User interfaces
Interfaces to other programs
Height of a rectangle
Height is an attribute of rectangle

Value of height is a number

Rectangle can record its height

Doc 11, Inheritance & Polymorphism Slide # 5Listen Here!
Finding Classes Other Views
Coad and Yourdon

Structure
"Kind of" and "part of" relationships

Other systems
External systems with which the application interacts

Devices
Devices with which the application interacts

Events remembered
Historical event that must be recorded

Roles played
Different roles users play in interacting with application

Locations
Physical locations, offices and sites important to the application
Organizational units


Doc 11, Inheritance & Polymorphism Slide # 6Listen Here!
Ross

People
Humans who carry out some function
Farmers

Places
Areas set aside for people or things
Fields on farm

Things
Physical objects or groups of objects
Plants
leaves roots

Organizations
Formally organized collections of people, resources, facilities
Union
farm crew

Concepts
Principles or ideas not tangible

Events
Things that happen


Doc 11, Inheritance & Polymorphism Slide # 7Listen Here!
Shaler & Mellor[3]

Tangible things
Cars
telemetry data

Roles
Mother
teacher

Events
Landing
interrupt

Interactions
Loan
meeting


Doc 11, Inheritance & Polymorphism Slide # 8Listen Here!

Relationships Between Classes


Is-kind-of or is-a
Implies inheritance
Place common responsibilities in superclass


is-analogous-to
If class X is-analogous-to class Y then look for superclass

is-part-of or has-a
If class A is-part-of class B then there is no inheritance
Some negotiation between A and B for responsibilities may be needed
Example:
Assume A contains a list that B uses

Who sorts the list? A or B?


Doc 11, Inheritance & Polymorphism Slide # 9Listen Here!

Banking Classes


Customer

Transaction

Currency



Doc 11, Inheritance & Polymorphism Slide # 10Listen Here!
Account
abstract class account {

   public static final boolean VALID = true;

   protected Customer accountOwner;
   protected Currency   balance;
   protected Stack accountHistory;

   // Other fields left out

   public void processTransaction( Transaction accountActivity ) {
      
      if ( validateTransaction( accountActivity ) == VALID )

         commitTransaction( accountActivity );

      else

         // report the problem. Code not shown
   }

   
   protected abstract boolean validateTransaction( Transaction 
                                       accountActivity 
                                      );

   protected abstract void commitTransaction( Transaction 
                                      accountActivity 
                                    );

   // other methods left out

}

Doc 11, Inheritance & Polymorphism Slide # 11Listen Here!
Account Class Inheritance

Doc 11, Inheritance & Polymorphism Slide # 12Listen Here!
Polymorphism
Account  newCustomer;

newCustomer = magicFunctionToCreateNewAccount()

newCustomer.processTransaction( amount );


Which processTransaction is called?



Adding new types of accounts to program requires:
Adding new subclasses
Changing code that creates objects


Doc 11, Inheritance & Polymorphism Slide # 13Listen Here!
Modular Design Rules

Avoid Case (and if) Statements

switch  (  newCustomer.accountType ) {

   case SAVINGS:
      // Code to process Savings account

   case CD:
      // Code to process CD account

   case CHECKING:
      // Code to process Checking account

   // etc.
}
Use Polymorphism
newCustomer.processTransaction( amount );


Supports:
Composability                                 Continuity                                    

Doc 11, Inheritance & Polymorphism Slide # 14Listen Here!

How to Increase the use of Polymorphism[4]




Protocol
Name and arguments of public operations of an object



Plug Compatible Objects
Objects with the same protocol
Standard Protocol

Using the same name for similar processes

Doc 11, Inheritance & Polymorphism Slide # 15Listen Here!
Rules for Finding Standard Protocols
(To Encourage Polymorphism)

Smalltalk methods average about 3 - 5 lines of code




Doc 11, Inheritance & Polymorphism Slide # 16Listen Here!
Rules for Finding Standard Protocols
(To Encourage Polymorphism)

Methods with small number of arguments are more likely to be usable in different classes
Reduce number of arguments by:
Breaking a message into several smaller messages
Creating a new class that represents a group of arguments



Doc 11, Inheritance & Polymorphism Slide # 17Listen Here!
Rules for Finding Standard Protocols
(To Encourage Polymorphism)

      if (anObject class = integer) then
         anObject.Foo;

      else
         anObject.Fee;
Implement Foo in all relevant classes to perform the equivalent operation
         anObject.Foo

Can be more difficult, but no less important


Doc 11, Inheritance & Polymorphism Slide # 18
Rules for Finding Standard Protocols
(To Encourage Polymorphism)

If an operation X is implemented by performing a similar operation on the component of the receiver, then that operation should also be named X.
void OrderedLinkedList::add(int value) 
{
   cell *newCell;

   newCell = new cell;
   newCell->value = value;
   listHead->add(*newCell);
};

Doc 11, Inheritance & Polymorphism Slide # 19Listen Here!
Modular Design Rules

Grow Well Shaped & Balanced Trees

Avoid Short Fat Trees
Avoid Tall Skinny Trees

Doc 11, Inheritance & Polymorphism Slide # 20Listen Here!

Metric Rules of Thumb[5]

Bigger averages indicate object-oriented design problems

Bigger averages indicate too much responsibility in too few classes

Bigger averages indicate that one class is doing more than it should

Start counting at the level of any framework classes you use or the root class if you don't


----------