Document 15071948

advertisement
Matakuliah
Tahun
: T0104 – Metode Perancangan Program
: 2010
THE CLASS RELATIONSHIPS
Pertemuan - 15
Pertemuan 15
The Class Relationships
Objectives
• To describe relationships between classes
• To introduce polymorphism and operation overriding in
object-oriented design
The Class Relationships
• Major advantage of object-oriented programming
language is their usefulness in constructing large
programs
• In designing programs that use multiple classes, need to
consider the relationship between the classes and
therefore between the objects that are instantiated from
those classes
The Class Relationships
• Notations
– Notation called standing for united modelling language (UML)
– UML allows a designer to represent the relationship between
classes as well as between objects
The Class Relationships
•
Three types of relationship between classes:
1. Relationship between two classes that are
independent of each other but one class
might use the services the other provides –
Association
2. Class may be made up of other classes or
contain other classes that are part of itself –
Aggregation or Composition
3. Class may inherit all the attributes and
operations of a parent class but is given a
unique name - Generalisation
The Class Relationships
• Association
– Association is a general binary relationship that describes an
activity between two classes
– Association between two classes is required when the classes
need to interact or communicate for the program to achieve its
purpose
– Association are also called links
– For example, a student taking a course is an association
between the Student class and the Course class, and a faculty
member teaching a course is an association between the Faculty
class and the Course class.
The Class Relationships
• A student may take any number of courses, and a faculty
member teaches at most three courses. A course may
have from five to sixty students and is taught by only one
faculty member.
The Class Relationships
• Association may exist between objects of the same
class.
• For example, a person may have a supervisor.
The Class Relationships
• Aggregation and composition
– Special forms of association that represents an ownership
relationship between two objects, where objects of one class, the
container class, are made up or consist of other objects from
other classes, the components classes
– Aggregation models has-a relationships. The owner object is
called an aggregating object, and its class, an aggregating class.
The subject object is called an aggregated object, and its class,
an aggregated class
– There are whole-part associations: one class is the whole that is
made up of parts
The Class Relationships
– Mildest form called aggregation, the part or component classes
that make up the whole are able to exist without necessarily
being part of the aggregation
– Strongest form called composition, the component classes can
only exist during the lifetime of the container object and cannot
exist outside the composition
The Class Relationships
• Dependency
– A dependency describes a relationship between two classes
where one (called client) uses the other (called supplier).
– In UML, draw a dashed line with an arrow from the client class to
the supplier class.
– For example, The Calendar class uses Date because you can
set a calendar with a specified Date object.
– The relationship between Calendar and Date can be described
using dependency, as shown
The Class Relationships
• Generalisation
– Class hierarchy that lets us group the shared attributes and
operations into a top-level class, and then to define one or
more lower-level classes with extra or different attributes and
operations
– The top-level class, also called the parent class or
superclass, has shared attributes and operations, and the
child classes or subclasses, inherit these, adding their own
attributes and operations to make them distinct
The Class Relationships
• Polymorphism
– Describes the use of operations of the same name for variety of
purposes
– Operating overrides occurs when a parent class provides an
operation, but the inheriting child class defines its own version of
that operation
– In another type of polymorphism, overloading, several operations
in a single class can have the same name
The Class Relationships
• Operation overriding
– Occurs when a parent class provides an operation but the
inheriting child class defines its own version of that operation
– The operation in a subclass will override the operation in the
superclass
The Class Relationships
• Operation overloading
– Occurs when several operations in a single class have the same
name but will act differently according to the number of
parameters that are passed to the operation when it is called
The Class Relationships
• Scope of data
– As soon as a project is created, the data that an object needs is
brought within its scope
– Attributes values are available to all the operations within the
class and are visible to each operation
Summary
• It is important to understand the relationships between
classes.
• An association relationship occurs when two classes are
independent of each other, but one class uses the
services that the other provides.
• The relationship between a parent class and a child
class is called generalisation.
Summary
• Polymorphism refers to the same method name being
used in different classes to perform a variety of
purposes.
• Operation overriding occurs when a parent class
provides an operation, but the inheriting class defines
its own version of the operation.
• Operation overloading occurs when several operations
in a single class have the same name, but will act
differently according to the number of parameters that
are passed to the operation when it is called.
Object Oriented Analysis and Modelling
Using The UML
Download