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 relationships are based on "is a" relationship, "has-a" relationship, and "part-of" relationship.
In this article, we will understand all these relationships.
Inheritance
Inheritance is an “IS-A” type of relationship. The “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 a type of B”. For example “Apple is a fruit”, and “Ferrari is a car”.
For better understanding let us take a real-world scenario.
- HOD is a staff member of the college.
- All teachers are staff members of the college.
- HOD and teachers have ID cards to enter into college.
- HOD has a staff that works according to the instructions of him.
- HOD has the responsibility to undertake the work of the teacher to cover the course in the fixed time period.
Let us take the first two assumptions, “HOD is a staff member of the college” and “All teachers are staff members of the college”. For this assumption, we can create a “StaffMember” parent class and inherit this parent class in the “HOD” and “Teacher” classes.
class StaffMember
{
public StaffMember()
{
}
}
class HOD : StaffMember
{
public HOD()
{
}
}
class Teacher : StaffMember
{
public Teacher()
{
}
}
Let us take an example for better understanding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
namespace Entity2
{
class StaffMember
{
public int MemberId { get; set; }
public string MemberName { get; set; }
public string Department { get; set; }
public StaffMember() { }
}
class HOD : StaffMember
{
public HOD() { }
public int Course_Completed { get; set; }
public void Hod_Info()
{
string Info = $"Member Id = {this.MemberId} \n Member Name = {this.MemberName} \n Department Name = {this.Department} \n Total Course Completed = {this.Course_Completed} %";
WriteLine(Info);
}
}
class Teacher : StaffMember
{
public Teacher() { }
public int Hod_Id { get; set; }
public void Teacher_Info()
{
string Info = $"Member Id = {this.MemberId} \n Member Name = {this.MemberName} \n Department Name = {this.Department} \n Id of HOD = {this.Hod_Id}";
WriteLine(Info);
}
}
class Program
{
static void Main(string[] args)
{
HOD Obj_Hod = new HOD();
Obj_Hod.MemberId = 10;
Obj_Hod.MemberName = "Dazy Arya";
Obj_Hod.Department = "CSE";
Obj_Hod.Course_Completed = 85;
Teacher Obj_Tech = new Teacher();
Obj_Tech.Department = "CSE";
Obj_Tech.MemberId = 15;
Obj_Tech.MemberName = "Ambika Gupta";
Obj_Tech.Hod_Id = 10;
Obj_Hod.Hod_Info();
Obj_Tech.Teacher_Info();
ReadLine();
}
}
}
Output
Composition
Composition is a "part-of" relationship. Simply composition means mean use of instance variables that are references to other objects. In a composition relationship, both entities are interdependent on each other for example “engine is part of car”, and “heart is part of body”.
Let us take an example of a car and an engine. Engine is a part of each car and both are dependent on each other.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
namespace Entity2
{
class Car
{
public Car() { }
public string Color { get; set; }
public string Max_Speed { get; set; }
}
class Suzuki : Car
{
public Suzuki() { }
public int Total_Seats { get; set; }
public string Model_No { get; set; }
public void CarInfo()
{
string Info = $"Color of car is {this.Color} \nMaximum speed is {this.Max_Speed}\nNumber of seats is {this.Total_Seats}\nModel No is {this.Model_No}\n";
WriteLine(Info);
Engine Obj = new Engine();
Obj.Engine_Info();
}
}
class Engine
{
public void Engine_Info()
{
WriteLine("Engine is 4 stroke and fuel efficiency is good");
}
}
class Program
{
static void Main(string[] args)
{
Suzuki Obj = new Suzuki();
Obj.Color = "Black";
Obj.Max_Speed = "240KM/Hour";
Obj.Model_No = "SUZ234";
Obj.Total_Seats = 4;
Obj.CarInfo();
ReadLine();
}
}
}
Output
Association
The association is a “has-a” type relationship. The association establishes the relationship b/w two classes using their objects. Association relationships can be one-to-one, to-many, many-to-one, and many-to-many. For example, suppose we have two classes then these two classes are said to have a “has-a” relationship if both of these entities share each other’s object for some work and at the same time they can exist without each other's dependency or both have their own lifetime.
Example
class Employee
{
public Employee() { }
public string Emp_Name { get; set; }
public void Manager_Name(Manager Obj)
{
Obj.manager_Info(this);
}
}
class Manager
{
public Manager() { }
public string Manager_Name { get; set; }
public void manager_Info(Employee Obj)
{
WriteLine($"Manager of Employee {Obj.Emp_Name} is {this.Manager_Name}");
}
}
class Program
{
static void Main(string[] args)
{
Manager Man_Obj = new Manager();
Man_Obj.Manager_Name = "Dazy Aray";
Employee Emp_Obj = new Employee();
Emp_Obj.Emp_Name = "Ambika";
Emp_Obj.Manager_Name(Man_Obj);
ReadLine();
}
}
Output
The above example shows an association relationship because both Employee and Manager classes use the object of each other and both their own independent life cycle.
Aggregation
Aggregation is based on a "has-a" relationship. Aggregation is a special form of association. In association, there is not any classes (entities) that work as owners but in aggregation one entity work as an 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 the relationship b/w Student class and Address class will be a “Has-A” type relationship but vice versa is not true(it is not necessary that each address contain by any student). So Students work as owner entities. This will be an aggregation relationship.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
namespace Entity2
{
class Student_
{
public Student_() { }
public string Name { get; set; }
public int roll_No { get; set; }
public int Class { get; set; }
public void Get_Student_Info(Address Obj)
{
WriteLine($"Student Name={this.Name}\n Roll_No={this.roll_No}\n Class={this.Class}\n");
Obj.Get_Address();
}
}
class Address
{
public Address() { }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Pincode { get; set; }
public void Get_Address()
{
WriteLine($"Street={this.Street} \n City={this.City} \n State={this.State}\n Pincode={this.Pincode}");
}
}
class Program
{
static void Main(string[] args)
{
Student_ Stu_Obj = new Student_();
Stu_Obj.Name = "Pankaj Choudhary";
Stu_Obj.roll_No = 1210038;
Stu_Obj.Class = 12;
Address Obj = new Address();
Obj.City = "Alwar";
Obj.Street = "P-20 Gandhi Nagar";
Obj.State = "Rajasthan";
Obj.Pincode = "301001";
Stu_Obj.Get_Student_Info(Obj);
ReadLine();
}
}
}
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.