Object Oriented Programming: Lecture-13 Instructor Name
Object Oriented Programming: Lecture-13 Instructor Name
Object Oriented Programming: Lecture-13 Instructor Name
Instructor Name:
Lecture-13
Today’s Lecture
Association
Aggregation vs Composition
2
Association
What is Association?
Interaction of different objects in OO model (or in problem domain)
is known as association.
1. Class Association
2. Object Association
3
Class Association
Class Association
Class Association is implemented in terms of Inheritance.
4
Object Association
Object Association
It is the interaction of stand alone objects of one class with other
objects of anther class.
1. Simple Association
2. Composition
3. Aggregation
5
Object Association
Simple Association
The two interacting objects have no intrinsic relationship with other object.
It is a reference by which one object can interact with some other object.
6
Object Association
Composition
An object may be composed of other smaller objects, the relationship
between the “part” objects and the “whole” object is known as Composition
7
Object Association
Composition
Composition is stronger relationship:
Composition is a stronger relationship, because Composed object becomes a
part of the composer
Composed object can’t exist independently
8
Object Association
Aggregation
An object may contain a collection (aggregate) of other objects, the
relationship between the container and the contained object is called
aggregation.
9
Object Association
Aggregation
Aggregation is weaker relationship, because
Example I
Furniture is not an intrinsic part of room
10
Object Association
An AlarmClock object needs to know the current time and the time when it’s
supposed to sound its alarm, so it’s reasonable to include two references to
Time objects in an AlarmClock object.
11
Composition
Class Date – declares instance variable month, day and year of type int to
represent a data.
12
Composition
13
Composition
14
Composition
The first two instance variable are reference to String object while the last
two are reference to Date object.
(shows a class can have instance variable of other class as we have already discussed in the clock
example Lecture: Modularization and Abstraction)
A constructor is written that receives four parameters and values are assigned
to the respectived reference objects
15
Composition
16
Composition
17
Composition
18
Composition
19
Composition
String city,state,country;
this.city = city;
this.state = state;
this.country = country;
20
Composition
e.display();
e2.display();
}
}
22
Coupling
What is Coupling?
Coupling is the degree of interdependence between software modules;
23
Coupling
What is Coupling?
Refer to Chapter Designing Classes, four exit variable are declared for south,
east, west north and in the setExits method there is one if statement per exit
and same is the case with goRoom and printLocation.
What if we need to add more exit variables to add new exits, we need to add
more cases
Solution : make use of hashMap that should cope with any number of exits,
and does not need so many modifications
24
Coupling
Loose Coupling
When only the implementation of a class changes, other classes should not be
affected
25
Cohesion
The rule of cohesion of classes states that each class should represent one
single, well-defined entity in the problem domain.
Advantages of Cohesion
Readability
Reuse
26
Enumeration
Enum in Java
Enum in java is a data type that contains fixed set of constants.
27
Enumeration
28
29