Object Oriented Programming: Lecture-13 Instructor Name

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Object Oriented Programming

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.

 In Object-oriented programming, one object is related to other to use


functionality and service provided by that object. This relationship
between two objects is known as the association

 There are two main types of association which re further subdivide as

1. Class Association

2. Object Association

3
Class Association

Class Association
 Class Association is implemented in terms of Inheritance.

 Inheritance implements generalization/specialization relationship


between objects.

 Inheritance is considered class association.

In case of public inheritance it is “IS-A” relationship.

 This relationship ensures that public members of base class are


available to derived class in case of public inheritance.

4
Object Association

Object Association
 It is the interaction of stand alone objects of one class with other
objects of anther class.

 It can be of following types

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 the weakest link between objects.

 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

 Composition is represented by a line with a filled-diamond head towards the


composer object

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.

 Aggregation is represented by a line with unfilled-diamond head towards the


container

9
Object Association

Aggregation
 Aggregation is weaker relationship, because

 Aggregate object is not a part of the container

 Aggregate object can exist independently

Example I
 Furniture is not an intrinsic part of room

 Furniture can be shifted to another room, and so can exist independent of


a particular room

10
Object Association

Class Composition – Case Study


 A class can have references to objects of other classes as members. This is
called composition and is sometimes referred to as a has-a relationship.

 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 Composition – Case Study


 Three Classes Date, Employee and Employee Test

 Class Date – declares instance variable month, day and year of type int to
represent a data.

 A constructor is written that receives three parameters and after validation


with the help of utility methods assign values to respective instance
variables.

 Three utility methods (checkDay(),checMonth(),checkYear()) , are written that


validate the input before assigning to any instance variable

 Class Date Code

12
Composition

Class Composition – Case Study

13
Composition

Class Composition – Case Study

14
Composition

Class Composition – Case Study


 Class Employee – declares instance variable firstName, lastName, birthdate,
hireDate.

 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

 Class Employee Code

15
Composition

Class Composition – Case Study

Composing object in the container


class

16
Composition

Class Composition – Case Study


 Class EmployeeTest – creates two date objects to represent employee date of
Birth and Hire date.

 Note Carefully the use of toString() Method.

17
Composition

Class Composition – Case Study

public class EmployeeTest


{
public static void main( String[] args )
{
Date birth =new Date(7,24,1989);
Date hire = new Date(3, 12, 2010 );
Employee employee = new Employee(
"Bob","Blue", birth, hire );
System.out.println( employee );
} // end main
}// end class EmployeeTest

18
Composition

Class Aggregation – Case Study


When Use Aggregation
 Code reuse is also best achieved by aggregation when there is no is-a
relationship.
 Inheritance should be used only if the relationship is-a is maintained
throughout the lifetime of the objects involved; otherwise, aggregation is the
best choice
 Next Discussed Code is borrowed from JavaPoint.com

19
Composition

Class Aggregation – Case Study


public class Address {

String city,state,country;

public Address(String city, String state, String


country) {

this.city = city;

this.state = state;

this.country = country;

20
Composition

Class Aggregation – Case Study


public class Emp {
int id;
String name;
Address address;

public Emp(int id, String name,Address address) {


this.id = id;
this.name = name;
this.address=address;
}
void display(){
System.out.println(id+" "+name);
System.out.println(address.city+"
"+address.state+" "+address.country);
}
}
21
Composition

Class Aggregation – Case Study


public class EmpTest{
public static void main(String[] args) {
Address address1=new Address("ABC","UP","Pak");
Address address2=new Address("XYZ","UP","Pak");

Emp e=new Emp(111,"Ali",address1);


Emp e2=new Emp(112,"Arnold",address2);

e.display();
e2.display();
}
}

22
Coupling

What is Coupling?
 Coupling is the degree of interdependence between software modules;

 a measure of how closely connected two routines or modules are;

 the strength of the relationships between 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

 Whenever a change is encountered there is always need of so many


modifications

 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

 This would be a case of loose coupling.

 What is the case in Designing Class Chapter? Tight Coupling

 Before implementing hashMap, first decouple classes

 Use of encapsulation in classes reduces coupling and thus leads to a better


design.

25
Cohesion

Cohesive Method & Class


 A cohesive method is responsible for one, and only one, well-defined task.

 A cohesive class represents one well-defined entity.

 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.

 It can be used for days of the week (SUNDAY, MONDAY, TUESDAY,


WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions
(NORTH, SOUTH, EAST and WEST) etc.

 The java enum constants are static and final implicitly.

 The declaration may optionally include other components of traditional


classes, such as constructors, fields and methods.

27
Enumeration

Restriction on Enum Class


1. Enum constants are implicitly final, because they declare constants that
shouldn’t be modified.

2. Enum constants are implicitly static.

3. Any attempt to create an object of an enum type with operator new


results in a compilation error.

28
29

You might also like