Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Lecture 15 Oop

The document discusses different types of relationships between classes in object-oriented programming including association, aggregation, composition, and inheritance. Association defines a non-ownership relationship between two classes. Aggregation and composition are special types of association where aggregation represents a weaker ownership relationship than composition.

Uploaded by

khanyz4884
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lecture 15 Oop

The document discusses different types of relationships between classes in object-oriented programming including association, aggregation, composition, and inheritance. Association defines a non-ownership relationship between two classes. Aggregation and composition are special types of association where aggregation represents a weaker ownership relationship than composition.

Uploaded by

khanyz4884
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Class Relationship

Association
Relationship where all object have their own lifecycle and
there is no owner.
Ex: Airplane and Passengers.
No ownership between the objects and both
have their own lifecycle. Both can create and
delete independently.
Association
• Association is a relationship between two objects.

• Association can be one-to-one, one-to-many, many-to-one,


many-to-many. Composition and aggregation are two form of
association.
Aggregation
Special form of Association where all object have their own
lifecycle but there is ownership ( Has a relationship) and
child object can not belong to another parent object.
Ex: Department and Teacher.

A single teacher can not belong to multiple departments, but if we delete

the department teacher object will not destroy.


Aggregation
• Aggregation is a special type of association between classes that
represents a weaker relationship than a composition.

• In an aggregation relationship, one class is a container for objects of


another class, but it is not responsible for the creation or destruction
of those objects.

• An object of one class containing pointers to objects of another class.


The contained objects are said to be part of the containing object, but
they can exist independently of the containing object.
Aggregation
• If the containing object is destroyed, the contained objects are not
automatically destroyed along with it.

• An association is said to be aggregation if both objects can exist


independently.
Aggregation
Example: A Team has 0 or
more players. A Player can
be in a Team or not. There
exists aggregation between
Team and Player.
Aggregation Code Example

Output
Task
• Write a C++ program to create two classes student and Teacher. Use
aggregation to solve it.
Composition

Special form of Aggregation . Child object dose not have


their lifecycle and if parent object deletes, all child object
will also be deleted (ownership).
Ex : Person and Hands.
Composition
• Composition is a strong association.

• An association is said to composition if an object owns another


object, and another object cannot exist without the owner object.
• In a composition relationship, one class is the owner of the other
class and is responsible for its creation and destruction.
Composition
• One class has an object of another class as a member.

• Member object cannot exist independently of the containing object.


If the containing object is destroyed, the contained object is
automatically destroyed along with it.
Composition

Example: Car class contains


Engine. Engine cannot exist
without a Car. There exists
composition between Car
and Engine.
Composition Code Example
Composition

Output
Task
• Write a C++ program to create two classes A and B.

• Use composition to create an object of class A in class B. Pass a value


from class B to A and display the value through class A.
Aggregation Vs Composition
Association

An association is usually represented as a data field in the class.

18
Representing Aggregation/Composition in Classes

An aggregation / composition relationship is usually represented as a data field in the aggregated class.

19
Inheritance(Generalization)
• The capability of a class to derive properties and characteristics from
another class is called Inheritance.

• Inheritance is a feature or a process in which, new classes are created


from the existing classes.
Inheritance
• The new class created is called “derived class” or “child class” and the
existing class is known as the “base class” or “parent class”.

• The derived class now is said to be inherited from the base class.

• When we say derived class inherits the base class, it means, the derived
class inherits all the properties of the base class, without changing the
properties of base class and may add new features to its own.
• These new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class. The base class is the
generalized class for the derived class.
Inheritance
• Sub Class: The class that inherits properties from another class is
called Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is
called Base Class or Superclass.

• Inheritance : ”is a” relationship


An employee is a Person
Why and when to use inheritance?
• Consider a group of vehicles.
• You need to create classes for Bus, Car, and Truck.
• The methods fuelAmount(), capacity(), apply Brakes() will be the
same for all three classes.
• If we create these classes avoiding inheritance, then we must write all
these functions in each of the three classes as shown below figure:
Why and when to use inheritance?
• You can clearly see that the above process results in duplication of
the same code 3 times.
• This increases the chances of error and data redundancy.
• To avoid this type of situation, inheritance is used.
• If we create a class Vehicle and write these three functions in it and
inherit the rest of the classes from the vehicle class, then we can
simply avoid the duplication of data and increase re-usability.
• Look at the below diagram in which the three classes are inherited
from vehicle class:
Inheritance vs Association

The Composition is a way to design or implement the "has-a"


relationship whereas, the Inheritance implements the "is-a"
relationship.
Inheritance Vs Composition
Association and Inheritance Example

Name Loan Person Borrower Address

27

You might also like