Object-Oriented Programming

UML Generalization

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

UML Class Relationships

Relationship Description
Dependency uses a
Generalization is a
Association has a
Aggregation (Association) has a
Composition (Association) has a

Generalization Terminology

Problems with Generalization

The purpose of generalization is to solve design problems. If you don't have a design problem, don't use generalization.

  • What design problems does generalization solve?

Scenario: Multiple Related Classes

Problems

  • If new kinds of students are added, we have to add more loops
  • Every new processing we add has to know about both undergraduate and graduate students
  • Have to remember to add new operations to both
  • Three different concepts:
    • Undergraduates
    • Graduates
    • Students - applies to both undergraduate and graduates

Solution

Solution

  • Client has a Student (Association) but is totally decoupled from Undergraduate/Graduate
  • Does Liskov hold? An is a relationship?
    • An undergraduate is a student
    • A graduate student is a student
  • Single piece of code to manage any type of Student
  • New kinds of Student can be added without changing the client code

Bidirectional Association

  • Customer has an Order
  • Order has a Customer

Problems with Bidirectional Associations

Solution

  • Extract only the part needed
  • Customer has an order
  • Order has a CustomerBase
  • Customer is a CustomerBase

Solution Code

Advantages

  • Develop and test class CustomerBase individually
  • Develop and test classes Order & CustomerBase together
  • Develop and test classes Customer & CustomerBase together
  • Often, class CustomerBase would be very small and simple, mostly to define an interface

Polymorphism

In general: the condition of occurring in several different forms

For design: single interface to entities of different types

  • Same interface for different types
  • Which code is run depends on the type of the object

Static Polymorphism

  • Determined at compile time
  • static dispatch
  • In C++:
    • overloading (function and operator)
    • templates

Without and With Method Overloading

Without and With Method Overloading

Method Overloading

  • Code is more flexible as parameter types can change
  • Easier to remember correct method names and parameters
  • Completely determined at compile-time
  • Can disguise significant differences in behavior
  • Can lead to bland and meaningless names, e.g., process()

Dynamic Polymorphism

  • Determined at run time
  • dynamic dispatch
  • In C++ requires virtual methods
  • Lots of flexibility
  • Slight speed disadvantage
  • Extra memory to support mechanism (vtables, vtable pointers)

Dynamic Polymorphism

Back To Students: What about Methods?

  • Of course, any Student object has costTuition()
  • Due to is_a relationship, so does any Undergraduate or Graduate object

Scenario

  • What if classes Undergraduate and Graduate need different implementations of costTuition()?
  • Could add the method to each (and implement it differently)

override

An override is when a derived class redefines a virtual method of the base class

  • With UML Generalization, we assume that all methods are virtual
  • Overriding is what provides the dynamic polymorphism
  • Non-virtual and static methods cannot be overridden in C++

C++

  • Once a method is virtual in a base class, it is virtual in all derived classes
  • The virtual specifier is often also applied to the method in the derived class, but that is not necessary. A better approach is to use the override specifier placed after the const.

Questions

  • Does it make sense to have a student who is neither an undergraduate nor a graduate student?
  • Perhaps all students should be one or the other? (at least for now)
  • How do these get formed?

Methods

Abstract Class

  • Consists of at least one pure-virtual method
  • Cannot create objects of that class type
  • Can only create objects of derived classes that implement all the pure-virtual methods

Interface

  • Consists of all pure-virtual methods
  • Sometimes, all empty methods
  • However, many times, the term is used interchangeably with Abstract Class

Virtual Design Choice

Pure Virtual Design

  • No pure CustomerBase objects
  • A derived class must override and implement all pure virtual methods to create objects
  • If all methods are pure virtual, then we potentially have an interface

Empty Virtual Design

  • Pure CustomerBase objects
  • A derived class picks and chooses which methods to override