- UML ( Unified Modelling Language )
- UML is a notation for representing OO designs
- It is a consensus among the OO community
- It is becoming an OMG (Object Management Group)
- It consists notations for class diagrams, use cases, sequence diagrams, and so on ...
class diagrams
Objects have attributes ( data ) and operations ( methods )
- attribute = slot, qualifier, field, instance variable
- operation = method, feature, procedure, function, action
Inheritance diagrams
- Inheritance allows one type to inherit behaviour from another
- Objects in the child class form a subset of objects in the parent class
- Inheritance is also referred to as generalisation and subtyping
Relations between Objects
- Association ( structural relationship ) = cross-reference, uses, instance connection, relationship
- Generalisation = inheritance, subtype
- Aggregation = inclusion, containing, part-whole, consists of
- Association and Links
Association says there is a relation between 2 classes
An attribute is an association where the class is ``small enough'' to fit
inside another class.
We denote it using:
rather than
Whereas an association refers to a relationship between
classes, the term link is used to refer to a structural relationship
that exists between two specific objects ( instances ).
A link is a specific instance of an association
with its member objects. i.e.
- An association is a potential relationship between objects of
a certain type/class.
- A link is an actual relationship between objects of those
particular types.
binary association -- association between two different classes
unary ( reflexive ) association -- association between two instances of same class
e.g.
- CS 201 ( a Course object ) is a prerequisite for CS 202
( another Course object )
ternary association, e.g.
- A Student attnds a Course
- A Professor teaches a Course
- A Professor advises a Student
- Aggregation
Aggregation is a special form of association. Maybe referred to
alternatively as:
- contains
- is composed of
- is comprised of
- is part of
Examples:
- A Car contains an Engine
- A Car contains a Transmission
- A Car contains a Body
- A Car is composed of many ( four, in this case ) Wheels
- Composition
Composition is the relation consists of where the components
cannot exist independently from the whole.
Wheels can exist independently from the car, and are not necessarily destroyed
when the car is wrecked
The car registration cannot exist without a car
- Multiplicity
- One-to-one (1:1)
- One-to-many (1:m)
- Many-to-many (m:m)
This can be shown on the association/aggregation/composition line, where `*' means zero or more
The following says that a Form can contain many (different) GUI objects,
including another Form. This is a potentially recursive structure
- Navigability
An association just says there is a relationship
The direction may be specified:
This has implementation implications:
class Car {
Wheel wheels[4];
}
class Wheel {
// no mention of Car
}
|
- Rules for Deriving Classes
The Do's:
- Extend the base class by adding features.
- You may specialize the way that a subclass performs one
or more of the services inherited from the parent class.
This is accomplished by overriding.
e.g.
parent has print() method
child ( sublcass ) has print() method but print differently
The Don'ts:
- Whenever possible, avoid having to add features to non-leaf nodes
once they have been established in code in an application ( because
this may affect the descendants ).
- You should not change the 'semantics' -- i.e. the intention, or
meaning -- of a feature.
e.g. if a print() prints something to console, its subclass
should not make print() printing data to a file.
- You cannot 'ovverride' an attribute.
- You cannot physically eliminate features, nor should you
effectively eliminate them by ignoring them.
- You cannot change the signature of a method.
You may create a different method entirely by overloading:
- overloading is supported by non-OO and OO languages.
- allows two or more different methods belonging to the
same class to have the same name as long as they have
different argument signature.
Example:
| boolean print(); | //no argument |
| void print( String filename ); | //a single argument |
| void print( int x ); | //a single argument |
| boolean print ( double d, String s ); | //two arguments |
- Multiple Inheritance
- complicated, difficult to keep track of parent
- Java language designers chose not to support multiple inheritance
- Java provides interface for handling the requirement of
creating an object with a 'split personality'.
Avoiding multiple inheritance