OOPS Unit 2 notes Saurabh Verma Sir
Notes
Relationships in Object-Oriented Programming
1.1 Meaning of Relationship
In Object-Oriented Programming, a relationship defines how two or more classes are logically
and functionally connected with each [Link] represents the way objects of one class interact,
communicate, or depend on objects of another [Link] help in modeling real-world
entities, such as how a Student is related to a College or a Car is related to an Engine.
Using relationships, complex software systems can be broken into smaller, manageable, and
reusable [Link] types of relationships in OOP include Inheritance,
Association, Aggregation, and Composition.
Importance of Relationships in OOP
1. Code Reusability
Code reusability means using existing code again without rewriting it.
Through relationships like inheritance, a child class can reuse the methods and variables of its
parent [Link] reduces duplicate code, saving both development time and [Link]
makes programs more consistent, since changes made in the parent class automatically reflect in
child [Link] also improves software reliability, as reused code is already tested and proven.
Example:
A class Vehicle can be reused by Car, Bike, and Bus classes.
2. Modularity
Modularity refers to dividing a large program into independent and smaller modules (classes).
Relationships help classes work together while remaining loosely [Link] class performs a
specific responsibility, making the system easier to [Link] one module needs changes,
other modules are not heavily [Link] approach improves team development, as different
developers can work on different modules.
Example:
A Library class interacts with Book and Member classes independently.
3. Maintainability
Maintainability means how easily a software system can be modified, updated, or [Link]
relationships are properly used, changes in one class do not break the entire [Link] fixing
becomes easier because issues can be isolated to specific [Link] reduce tight
dependency, making the system flexible and [Link]-maintained systems have lower
long-term development costs.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
Example:
Updating a Payment class does not affect the Customer class directly.
4. Real-World Modeling
Relationships allow programmers to represent real-world scenarios naturally in code.
Objects and their interactions closely resemble real-life entities and [Link] makes the
system easier to design, understand, and [Link]-world modeling improves clarity and
correctness of software [Link] helps in building scalable and realistic applications.
Example:
A Doctor class associated with Patient, or a Department class aggregating Employees.
2. Inheritance
2.1 Definition of Inheritance
Inheritance is a fundamental concept of Object-Oriented Programming in which one class
acquires the properties (data members) and behaviors (methods) of another [Link] class
whose properties are inherited is called the Parent Class, also known as Superclass or Base
Class.
The class that inherits the properties is called the Child Class, also known as Subclass or
Derived Class.
Inheritance establishes an IS-A relationship between the parent and child classes.
This mechanism promotes code reuse and logical program structure, making software easier
to manage and extend.
Terminology
Parent Class / Superclass / Base Class
The class whose properties and methods are inherited.
Child Class / Subclass / Derived Class
The class that inherits and can extend or modify the behavior of the parent class.
2.2 Purpose of Inheritance
1. Reuse Existing Code
Inheritance allows a new class to reuse the already written and tested code of an existing
class.
The child class automatically gets access to the public and protected members of the parent class.
This avoids rewriting the same methods multiple times.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
Reusing code improves development speed and consistency across the application.
It also reduces chances of errors since reused code is already reliable.
Example:
A Car class reuses common features from a Vehicle class.
2. Reduce Redundancy
Redundancy refers to duplicate code appearing in multiple classes.
Inheritance helps remove redundancy by placing common code in a single parent class.
All child classes can then share this common functionality.
This leads to cleaner and more organized code.
Less redundancy makes the program easier to modify and debug.
Example:
Common attributes like speed and fuel are written once in Vehicle instead of repeating in Car,
Bike, and Bus.
3. Enable Method Overriding
Inheritance allows a child class to override methods of the parent class.
Method overriding enables runtime polymorphism, where the method call is resolved at
execution time.
The child class can provide its own specific implementation of a method.
This increases flexibility and supports dynamic behavior in programs.
Overriding helps customize functionality without changing the parent class.
Example:
A draw() method behaves differently in Circle and Rectangle classes.
4. Improve Extensibility
Extensibility means the ability to add new features without affecting existing code.
Inheritance allows developers to create new classes by extending existing ones.
The original class remains unchanged, ensuring system stability.
This supports the Open–Closed Principle, where software is open for extension but closed for
modification.
It makes large systems easier to upgrade and scale.
Example:
Adding a new ElectricCar class without modifying the Vehicle class.
5. Create Hierarchical Classification
Inheritance helps organize classes into a logical hierarchy.
This hierarchy represents real-world relationships in a structured manner.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
It improves readability and understanding of the system design.
Hierarchical classification makes programs more systematic and easier to maintain.
It is especially useful in large applications with many related classes.
Example:
Animal → Mammal → Dog
Types of Inheritance
1. Single Inheritance
Definition
In Single Inheritance, one derived (child) class inherits from one base (parent) class.
Diagram
Features
Simplest form of inheritance
Promotes basic code reuse
Easy to understand and implement
C++ Example
class Vehicle {
public:
void start() {
cout << "Vehicle started";
OOPS Unit 2 notes Saurabh Verma Sir
Notes
}
};
class Car : public Vehicle {
};
2. Multilevel Inheritance
Definition
In Multilevel Inheritance, a class is derived from another derived class, forming a chain.
Diagram
Features
Represents real-world hierarchy
Supports step-by-step inheritance
Increases reusability across levels
C++ Example
class Animal {
public:
void eat() {
cout << "Eating";
}
};
class Mammal : public Animal {
};
class Dog : public Mammal {
};
OOPS Unit 2 notes Saurabh Verma Sir
Notes
3. Hierarchical Inheritance
Definition
In Hierarchical Inheritance, multiple child classes inherit from a single parent class.
Diagram
Features
Common behavior shared by many classes
Reduces code duplication
Widely used in large systems
C++ Example
class Shape {
public:
void draw() {
cout << "Drawing shape";
}
};
class Circle : public Shape {
};
class Rectangle : public Shape {
};
OOPS Unit 2 notes Saurabh Verma Sir
Notes
4. Multiple Inheritance
Definition
In Multiple Inheritance, one derived class inherits from more than one base class.
Diagram
Features
Powerful but complex
Can cause ambiguity problem
Solved using scope resolution operator (::)
C++ Example
class Engine {
public:
void engineType() {
cout << "Petrol Engine";
}
};
class Wheels {
public:
void wheelCount() {
cout << "Four wheels";
}
};
class Car : public Engine, public Wheels {
};
OOPS Unit 2 notes Saurabh Verma Sir
Notes
5. Hybrid Inheritance
Definition
Hybrid Inheritance is a combination of two or more types of inheritance.
Diagram
Features
Combines flexibility of multiple inheritance
Can become complex
Often uses virtual base classes to avoid ambiguity
C++ Example
class Person {
public:
void show() {
cout << "Person";
}
};
class Teacher : public Person {
};
class Student : public Person {
};
class TeachingAssistant : public Teacher, public Student {
};
OOPS Unit 2 notes Saurabh Verma Sir
Notes
Diamond Problem
Occurs in Hybrid / Multiple Inheritance when a class inherits the same base class multiple
times.
Solution
Use virtual inheritance:
class Person {
};
class Teacher : virtual public Person {
};
class Student : virtual public Person {
};
Diamond Problem
The Diamond Problem is a common issue in Multiple and Hybrid Inheritance in C++.
It occurs when a derived class inherits from two classes that both inherit from the same base
class.
As a result, the derived class gets multiple copies of the base class, leading to ambiguity.
The compiler cannot decide which base class member to access.
This problem mainly arises due to duplicate inheritance paths.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
Why It Is Called “Diamond” Problem
The structure of inheritance forms a diamond shape when represented graphically.
The base class appears at the top, two intermediate classes in the middle, and one derived class at
the bottom.
Because of this shape, the problem is named Diamond Problem.
The base class (Person) gets inherited twice by the bottom class.
This duplication causes confusion for the compiler.
Problem Without Virtual Inheritance
Code Example (Problematic)
class Person {
public:
void display() {
cout << "Person class";
}
};
class Teacher : public Person {
};
class Student : public Person {
};
class TeachingAssistant : public Teacher, public Student {
};
Issue Explanation
TeachingAssistant inherits two copies of Person
Calling display() causes ambiguity
Compiler error: ambiguous base of Person
Solution: Virtual Inheritance
Definition
Virtual inheritance ensures that only one shared copy of the base class is inherited.
It tells the compiler to delay the creation of the base class until the most derived class.
This removes duplication and ambiguity.
Virtual inheritance is the standard solution to the Diamond Problem in C++.
It is implemented using the virtual keyword.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
Code with Virtual Inheritance (Correct)
class Person {
public:
void display() {
cout << "Person class";
}
};
class Teacher : virtual public Person {
};
class Student : virtual public Person {
};
class TeachingAssistant : public Teacher, public Student {
};
How Virtual Inheritance Solves the Problem
Only one instance of Person is created
Both Teacher and Student share the same base object
Ambiguity is removed
Memory usage becomes efficient
Program becomes compiler-safe
3. “IS-A” Relationship
3.1 Definition
The IS-A relationship represents inheritance in Object-Oriented Programming.
It indicates that one class is a specialized form or type of another class.
In an IS-A relationship, the child class inherits properties and behaviors of the parent
class.
This relationship establishes a parent–child hierarchy between classes.
IS-A relationship improves code reuse, extensibility, and logical program structure.
In C++, the IS-A relationship is implemented using the colon (:) inheritance syntax, while
in Java it is implemented using the extends keyword.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
3.2 Examples of IS-A Relationship
Example 1: Dog IS-A Animal
A Dog is a type of Animal, so it naturally follows an IS-A relationship.
The Dog class can inherit common characteristics such as eat() and sleep() from the
Animal class.
This avoids duplication of common behavior across different animals.
The Dog class can also add or override methods to provide dog-specific behavior.
This makes the program flexible and easy to extend.
C++ Example
class Animal {
public:
void eat() {
cout << "Animal eats";
}
};
class Dog : public Animal {
};
Example 2: Student IS-A Person
A Student is a specific type of Person, so it satisfies the IS-A condition.
The Student class inherits common attributes like name and age from the Person class.
This relationship helps represent real-world systems accurately.
Additional student-specific behavior can be added without modifying the Person class.
Such design supports clean and maintainable code.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
C++ Example
class Person {
public:
void display() {
cout << "Person details";
}
};
class Student : public Person {
};
3.3 Characteristics of IS-A Relationship
1. Supports Method Overriding
In an IS-A relationship, the child class can override methods of the parent class.
This allows the child class to provide its own implementation of inherited methods.
Method overriding enables dynamic behavior at runtime.
It helps achieve polymorphism, one of the core principles of OOP.
Overriding improves flexibility without modifying the base class.
2. Enables Runtime Polymorphism
IS-A relationship is essential for runtime polymorphism.
A parent class reference can refer to a child class object.
The method call is resolved at runtime, not at compile time.
This allows different objects to respond differently to the same method call.
Runtime polymorphism makes systems scalable and adaptable.
4. Association
4.1 Definition
Association represents a relationship where objects of different classes communicate or
interact with each other.
It shows how one class uses another class to perform a task.
Association represents a uses-a relationship, not ownership.
In association, both objects have independent existence and lifecycles.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
If one object is destroyed, the other object continues to exist.
Association is the most general relationship in Object-Oriented Programming.
4.2 Types of Association
1. One-to-One Association
In One-to-One association, one object is associated with exactly one object of another class.
Each object has a single corresponding object.
This relationship is commonly used when two entities are closely related but independent.
Both objects can exist separately.
It is simple and easy to implement.
Example:
Person → Passport
C++ Example
class Passport {
};
class Person {
Passport p;
};
2. One-to-Many Association
OOPS Unit 2 notes Saurabh Verma Sir
Notes
In One-to-Many association, one object is associated with multiple objects of another class.
The parent object interacts with many related child objects.
This type is widely used in real-world applications.
Objects still have independent lifecycles.
It improves data organization and clarity.
Example:
Teacher → Students
C++ Example
class Student {
};
class Teacher {
Student s[30];
};
3. Many-to-Many Association
In Many-to-Many association, multiple objects of one class are associated with multiple
objects of another class.
This relationship is commonly implemented using collections or intermediate classes.
It reflects complex real-world interactions.
Both sides can initiate communication.
Lifecycle independence is maintained.
Example:
Students ↔ Courses
C++ Example
class Course {
};
class Student {
Course c[5];
};
4.3 Characteristics of Association
1. Loose Coupling
Association promotes loose coupling between classes.
Classes are not tightly dependent on each other.
Changes in one class do not heavily affect the other.
Loose coupling improves flexibility and scalability.
It makes systems easier to maintain.
OOPS Unit 2 notes Saurabh Verma Sir
Notes
5. Aggregation
5.1 Definition
Aggregation is a special form of association that represents a HAS-A relationship.
It models a whole–part relationship between objects.
In aggregation, the child object can exist independently of the parent object.
The relationship is weak, meaning no strong ownership.
Aggregation improves structural clarity in object design.
5.2 Examples of Aggregation
Example 1: Department HAS-A Teacher
A Department consists of Teachers, but teachers can exist without the department.
If the department is removed, teachers still exist.
This shows a weak ownership relationship.
Department groups teachers but does not own their lifecycle.
Hence, this is aggregation.
C++ Example
class Teacher {
};
class Department {
Teacher* t;
};
OOPS Unit 2 notes Saurabh Verma Sir
Notes
Example 2: Library HAS-A Books
A Library contains Books, but books can exist outside the library.
Books may be transferred, sold, or stored elsewhere.
The library does not control the complete lifecycle of books.
This makes the relationship weak.
Therefore, it is aggregation.
C++ Example
class Book {
};
class Library {
Book* b;
};
5.3 Characteristics of Aggregation
1. Whole–Part Relationship
Aggregation represents a whole–part structure.
The parent object acts as a container.
The child object represents a part of the whole.
However, the child is not fully dependent on the parent.
This mirrors real-world grouping relationships.
2. No Complete Ownership
In aggregation, the parent does not completely own the child object.
The child can exist even after the parent is destroyed.
Memory management remains flexible.
This avoids tight dependency.
It enhances object reusability.
3. UML Representation
Aggregation is represented in UML using a hollow diamond (◇).
The diamond is placed on the parent (whole) side.
It visually distinguishes aggregation from composition.
UML representation helps in system design clarity.
It is frequently asked in exams.
Association vs Aggregation
Feature Association Aggregation
Type Association is a general relationship in Aggregation is a special type of
OOPS Unit 2 notes Saurabh Verma Sir
Notes
OOP. It represents simple interaction association. It represents a whole–
between objects. There is no concept of part relationship between objects. It
ownership or whole–part structure. It is adds more meaning than simple
the most basic form of relationship used interaction. The relationship shows
to connect classes. Objects only grouping of objects. It is more specific
communicate with each other. than association.
Dependency Objects in association are completely Objects in aggregation are partially
independent. Creation or deletion of dependent. The child object depends
one object does not affect the other. logically on the parent but is not
There is no lifecycle dependency. This owned fully. If the parent is destroyed,
provides high flexibility in system the child can still exist. This creates a
design. Association is suitable for weak dependency. Aggregation shows
temporary interactions. controlled dependency.
Meaning Association represents a Uses-A Aggregation represents a Has-A
relationship. One object uses another relationship. One object contains or
object to perform an operation. There is groups another object. It shows
no containment or ownership involved. structural organization. However, the
The relationship focuses on parent does not fully own the child.
functionality. It is commonly used in Aggregation reflects logical
service-based designs. containment.
Strength Association is a weak relationship. It Aggregation is stronger than
only indicates interaction between association but weaker than
objects. There is no strong bond composition. It represents a
between classes. The relationship can be meaningful whole–part relationship.
removed easily. It provides loose The connection has structural
coupling. importance. Still, ownership is not
strict. It balances flexibility and
structure.