Relationships Among Classes - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Relationships Among Classes

Description:

An association relationship between two classes may be identified early in analysis. ... polymorphism: we can send the set message to instances of either class. ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 28
Provided by: boba2
Category:

less

Transcript and Presenter's Notes

Title: Relationships Among Classes


1
Lesson 4
  • Relationships Among Classes

2
Association
  • Association is a relationship where two classes
    are weakly connected i.e. they are merely
    associates.
  • The dependency of one class on another is
    bidirectional
  • There is no hierarchy.
  • The relationship is between equals.

3
Association (continued)
  • The association has a cardinality
  • one-to-one
  • one-to-many
  • many-to-many
  • An association relationship between two classes
    may be identified early in analysis.
  • Later analysis may refine the relationship, and
    identify it to be another kind (described below).

4
(c) 1994 Booch
5
Inheritance
  • Inheritance is a relationship among classes where
    a subclass inherits the structure and behavior of
    its superclass.
  • Defines the is a or generalization/specializatio
    n hierarchy.
  • Structure instance variables.
  • Behavior instance methods.

6
Inheritance (continued)
  • A subclass may augment the existing structure and
    behavior of its superclass.
  • A subclass may constrain or restrict the behavior
    inherited from its superclass.
  • Single inheritance any class has at most one
    immediate superclass.

7
(c) 1994 Booch
8
Inheritance (continued)
  • Concrete or leaf classes classes that will have
    instances.
  • Most classes are concrete.
  • Abstract classes classes with no instances.
  • Expected that subclasses will add structure and
    behavior to the abstract class.
  • The subclasses will have instances (i.e. are
    concrete).

9
Inheritance (continued)
  • Base class the root of the class hierarchy.
  • Is the most general class.
  • In Java, Object is the base class.
  • In C, you can have forests of inheritance
    trees, thus several base classes.

10
Inheritance (continued)
  • In Java and C
  • A superclass is referred to with the keyword
    super.
  • An object refers to itself with the keyword this.

11
Inheritance (continued)
  • Polymorphism where the same message can be sent
    to two objects of different classes.
  • Works because the classes are related in the
    inheritance tree i.e. there is a common
    superclass where the method is defined.
  • The subclasses redefine the method, so that
    behavior is specialized in some way.

12
Inheritance (continued)
  • A subclass augments the behavior of its
    superclass by redefining the superclasss
    methods.
  • The subclasss method will invoke the
    superclasss method either before or after doing
    some other action.
  • E.g.

13
Inheritance (continued)
  • Note the polymorphism we can send the set
    message to instances of either class.
  • Note the code reuse.
  • A subclass augments the structure of its
    superclass by declaring additional instance
    variables.
  • E.g.

14
Inheritance (continued)
  • A superclasss instance variables are directly
    accessible to the subclass if they are declared
    protected.
  • If declared private, these variables can only be
    accessed by sending messages to super.
  • It is usually poor practice to declare instance
    variables public.

15
Inheritance (continued)
  • Multiple Inheritance where a subclass may have
    more than one immediate superclass.
  • Java does not support multiple inheritance, but
    C supports it.

16
(c) 1994 Booch
17
Aggregation
  • Denotes a whole/part hierarchy among classes.
  • Is directly parallel to aggregation relationships
    between objects.
  • One or more objects form part of some container
    object.
  • The part objects are created and destroyed when
    the container object is created and destroyed.

18
Aggregation (continued)
  • In Java, only containment by reference is used,
    since objects are created at runtime.
  • In C, containment by value is allowed, since
    objects can be created at compile time as well as
    run time.

19
(c) 1994 Booch
20
Using
  • When there is a peer-to-peer link between two
    objects, a using relationship exists between
    the two corresponding classes.
  • One class is said to use the other class (i.e.
    the client uses the supplier).

21
(c) 1994 Booch
22
Instantiation
  • A parameterize class (also known as a generic
    class) is one that serves as a template for other
    classes a template that may be parameterized by
    other classes, objects and /or operations. A
    parameterized class must be instantiated (that
    is, its parameters must be filled in) before
    objects can be created. C and Eiffel both
    support generic class mechanisms.
  • e.g. Queueltintgt intQueue
  • QueueltDisplayItem gt itemQueue

23
Instantiation
  • In C, a parameterized or generic class can
    serve as a template for other classes.
  • When an object is to be instantiated, the generic
    class is given some parameters to complete the
    class definition.
  • E.g. Queueltintgt intQueue
  • Java does not support generic classes.

24
(c) 1994 Booch
25
Metaclass
  • A metaclass is the class of a class.
  • You can treat the class as an object, and send
    messages to it.
  • Best supported in Smalltalk and CLOS.
  • Indirectly supported in Java using class methods
    and class variables.

26
(c) 1994 Booch
27
This presentation is based on the following book
Object Oriented Analysis and Design by G.Booch
and partially compiled by Leonard Manzara.
Write a Comment
User Comments (0)
About PowerShow.com