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.





Abhijeet ChoudhariPosted Jul 12, 2020, 2:12 PM
Nice article. So from above example can i say both the example in association and aggregation are type of aggregation example. As student must have address and vice versa not possible ...same every manager is employee but not vice versa possible.. please correct me if i m wrong
Trực Lê CôngPosted Dec 13, 2017, 10:45 PM
You can talk what is"the relationship b/w the classes" ? . tks.
Trực Lê CôngPosted Dec 13, 2017, 10:44 PM
Tks admin for topic.
Karthik ElumalaiPosted Nov 14, 2016, 8:00 PM
Its really good and thanks for this article
Ankur VermaPosted Sep 29, 2016, 6:02 AM
Good
Delpin Susai RajPosted Aug 28, 2016, 9:39 AM
Nice
Anu VPosted May 19, 2016, 6:17 AM
Nice
Kuppurasu NagarajPosted May 17, 2016, 11:33 AM
Nice sharing..
Sonu ChaudharyPosted May 16, 2016, 3:34 PM
good one...
Prasanna MuraliPosted May 16, 2016, 10:06 AM
Nice one...
Debasis SahaPosted May 16, 2016, 12:52 AM
Nice one..
Pankaj Kumar ChoudharyPosted May 15, 2016, 11:48 PM
Thanks to all of you........
Pankaj Kumar ChoudharyPosted May 15, 2016, 11:48 PM
Thanks Michael Griffiths
Talha Bin AfzalPosted May 15, 2016, 10:22 PM
Great
Pradeep SahooPosted May 14, 2016, 11:35 PM
Nice article.Thanks for sharing.............
Neeraj KumarPosted May 14, 2016, 3:56 PM
Good Pankaj Kumar Choudhary
Pankaj SharmaPosted May 14, 2016, 12:47 PM
Nice Explain Pankaj.......
Prasanna MuraliPosted May 14, 2016, 9:30 AM
Nice one.....
Michael GriffithsPosted May 14, 2016, 6:51 AM
Nice