Types Of Relationships In Object Oriented Programming (OOPS)

One of the advantages of Object-Oriented programming language is code reuse. This reusability is possible due to the relationship b/w the classes. Object oriented programming generally support 4 types of relationships that are: inheritance , association, composition and aggregation. All these relationship is based on "is a" relationship, "has-a" relationship and "part-of" relationship.

In this article we will understand all these relationships.

Inheritance:

Inheritance is “IS-A” type of relationship. “IS-A” relationship is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. Inheritance is a parent-child relationship where we create a new class by using existing class code. It is just like saying that “A is type of B”. For example is “Apple is a fruit”, “Ferrari is a car”.

For better understanding let us take a real world scenario.

  • HOD is a staff member of college.
  • All teachers are staff member of college.
  • HOD and teachers has id card to enter into college.
  • HOD has a staff that work according the instruction of him.
  • HOD has responsibility to undertake the works of teacher to cover the course in fixed time period.

Let us take first two assumptions , “HOD is a staff member of college” and “All teachers are staff member of college”. For this assumption we can create a “StaffMember” parent class and inherit this parent class in “HOD” and “Teacher” class.

  1. class StaffMember  
  2.     {  
  3.         public StaffMember()  
  4.         {  
  5.   
  6.         }  
  7.     }  
  8.   
  9.     class HOD : StaffMember  
  10.     {  
  11.         public HOD()  
  12.         {  
  13.   
  14.         }  
  15.     }  
  16.   
  17.     class Teacher : StaffMember  
  18.     {  
  19.         public Teacher()  
  20.         {  
  21.   
  22.         }  
  23.     }  
Let us take an example for better understanding.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using static System.Console;  
  6.   
  7. namespace Entity2  
  8. {  
  9.    class StaffMember  
  10.     {  
  11.         public int MemberId {getset;}  
  12.         public string MemberName { getset;}  
  13.         public string Department { getset; }  
  14.         public StaffMember()  
  15.         {  
  16.   
  17.         }  
  18.     }  
  19.   
  20.     class HOD : StaffMember  
  21.     {  
  22.         public HOD()  
  23.         {  
  24.   
  25.         }  
  26.         public int Course_Completed { getset; }  
  27.   
  28.         public void Hod_Info()  
  29.         {  
  30.             string Info = $"Member Id ={this.MemberId} \n Member Name={this.MemberName} \n Department Name={this.Department} \n Total Course Completed ={this.Course_Completed} %";  
  31.             WriteLine(Info);  
  32.         }  
  33.     }  
  34.   
  35.     class Teacher : StaffMember  
  36.     {  
  37.         public Teacher()  
  38.         {  
  39.   
  40.         }  
  41.         public int Hod_Id { getset; }  
  42.         public void Teacher_Info()  
  43.         {  
  44.             string Info = $"Member Id ={this.MemberId} \n Member Name={this.MemberName} \n Department Name={this.Department} \n Id of HOD ={this.Hod_Id} ";  
  45.             WriteLine(Info);  
  46.         }  
  47.   
  48.     }  
  49.       
  50.      
  51.     class Program  
  52.     {  
  53.         static void Main(string[] args)  
  54.         {  
  55.             HOD Obj_Hod = new HOD();  
  56.             Obj_Hod.MemberId = 10;  
  57.             Obj_Hod.MemberName = "Dazy Arya";  
  58.             Obj_Hod.Department = "CSE";  
  59.             Obj_Hod.Course_Completed = 85;  
  60.   
  61.             Teacher Obj_Tech = new Teacher();  
  62.             Obj_Tech.Department = "CSE";  
  63.             Obj_Tech.MemberId = 15;  
  64.             Obj_Tech.MemberName = "Ambika Gupta";  
  65.             Obj_Tech.Hod_Id = 10;  
  66.   
  67.             Obj_Hod.Hod_Info();  
  68.             Obj_Tech.Teacher_Info();  
  69.             ReadLine();  
  70.         }  
  71.     }  
  72.   
  73. }  
Output

Output

Composition:

Composition is a "part-of" relationship. Simply composition means mean use of instance variables that are references to other objects. In composition relationship both entities are interdependent of each other for example “engine is part of car”, “heart is part of body”.

Let us take an example of car and engine. Engine is a part of each car and both are dependent on each other.
example
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using static System.Console;  
  6.   
  7. namespace Entity2  
  8. {  
  9.    class Car{  
  10.         public Car() { }  
  11.         public string Color { getset; }  
  12.         public string Max_Speed { getset; }  
  13.     }  
  14.       
  15.   
  16.     class Suzuki:Car  
  17.     {  
  18.         public Suzuki() { }  
  19.         public int Total_Seats { getset; }  
  20.         public string Model_No { getset; }  
  21.         public void CarInfo()  
  22.         {  
  23.             string Info=$"Color Of car is {this.Color} \n Maximum Speed is {this.Max_Speed}\n Numbers of Seets is\n  {this.Total_Seats} Model_No is {this.Model_No}  \n";  
  24.             WriteLine(Info);  
  25.             Engine Obj = new Engine();  
  26.             Obj.Engine_Info();  
  27.         }  
  28.     }  
  29.      
  30.     class Engine  
  31.     {  
  32.         public void Engine_Info()  
  33.         {  
  34.             WriteLine("Engine is 4 stroke and fuel efficiency is good");  
  35.         }  
  36.     }  
  37.     class Program  
  38.     {  
  39.         static void Main(string[] args)  
  40.         {  
  41.             Suzuki Obj = new Suzuki();  
  42.             Obj.Color = "Black";  
  43.             Obj.Max_Speed = "240KM/Hour";  
  44.             Obj.Model_No = "SUZ234";  
  45.             Obj.Total_Seats = 4;  
  46.             Obj.CarInfo();  
  47.             ReadLine();  
  48.         }  
  49.     }  
  50.   
  51. }  
Output

Output

Association:

Association is a “has-a” type relationship. Association establish the relationship b/w two classes using through their objects. Association relationship can be one to one, One to many, many to one and many to many. For example suppose we have two classes then these two classes are said to be “has-a” relationship if both of these entities share each other’s object for some work and at the same time they can exists without each others dependency or both have their own life time.

Example
  1. class Employee  
  2.     {  
  3.         public Employee() { }  
  4.         public string Emp_Name { getset; }  
  5.   
  6.         public void Manager_Name(Manager Obj)  
  7.         {  
  8.             Obj.manager_Info(this);  
  9.         }  
  10.     }  
  11.   
  12.     class Manager  
  13.     {  
  14.          
  15.         public Manager() { }  
  16.         public string Manager_Name { getset; }  
  17.         public void manager_Info(Employee Obj)  
  18.         {  
  19.             WriteLine($"Manager of Employee  {Obj.Emp_Name} is  {this.Manager_Name}");  
  20.   
  21.         }  
  22.   
  23.   
  24.   
  25.     }  
  26.     class Program  
  27.     {  
  28.         
  29.   
  30.         static void Main(string[] args)  
  31.         {  
  32.             Manager Man_Obj = new Manager();  
  33.             Man_Obj.Manager_Name = "Dazy Aray";  
  34.             Employee Emp_Obj = new Employee();  
  35.             Emp_Obj.Emp_Name = "Ambika";  
  36.             Emp_Obj.Manager_Name(Man_Obj);             
  37.             ReadLine();  
  38.         }  
  39.     }  
Output

Output

Above example showing an association relationship because both Employee and Manager class using the object of each other and both a their own independent life cycle.

Aggregation

Aggregation is based is on "has-a" relationship. Aggregation is a special form of association. In association there is not any classes (entity) work as owner but in aggregation one entity work as owner. In aggregation both entities meet for some work and then get separated. Aggregation is a one way association.

Example

Let us take an example of “Student” and “address”. Each student must have an address so relationship b/w Student class and Address class will be “Has-A” type relationship but vice versa is not true(it is not necessary that each address contain by any student). So Student work as owner entity. This will be a aggregation relationship.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using static System.Console;  
  6.   
  7. namespace Entity2  
  8. {  
  9.        class Student_  
  10.     {  
  11.         public Student_() { }  
  12.         public string Name { getset; }  
  13.         public int roll_No { getset; }  
  14.         public int Class { getset; }  
  15.         public void Get_Student_Info(Address Obj)  
  16.         {  
  17.             WriteLine($"Student Name={this.Name}\n Roll_No={this.roll_No}\n Class={this.Class}\n");  
  18.             Obj.Get_Address();  
  19.         }  
  20.     }  
  21.       
  22.     class Address  
  23.     {  
  24.         public Address() { }  
  25.         public String Street { getset; }  
  26.         public string City { getset; }  
  27.         public string State { getset; }  
  28.         public string Pincode { getset; }  
  29.         public void Get_Address()  
  30.         {  
  31.             WriteLine($"Street={this.Street} \n City={this.City} \n State={this.State}\n Pincode={this.Pincode}");  
  32.         }  
  33.     }  
  34.     class Program  
  35.     {   
  36.         static void Main(string[] args)  
  37.         {  
  38.             Student_ Stu_Obj=new Student_();  
  39.             Stu_Obj.Name = "Pankaj Choudhary";  
  40.             Stu_Obj.roll_No = 1210038;  
  41.             Stu_Obj.Class = 12;  
  42.             Address Obj = new Address();  
  43.             Obj.City = "Alwar";  
  44.             Obj.Street = "P-20 Gnadhi Nagar";  
  45.             Obj.State = "Rajasthan";  
  46.             Obj.Pincode = "301001";  
  47.             Stu_Obj.Get_Student_Info(Obj);  
  48.             ReadLine();  
  49.         }  
  50.     }  
  51.   
  52. }  
Output

output

Generally, these four types of relationships (Inheritance, Composition, Association and Aggregation) are used in OOPS. If you have any doubt or question then mention it in the comment section.