Types of relationships establishable with classes in C#

David Ukpoju
4 min readNov 21, 2022

--

The relationships between classes are considered or known as the building blocks of object-oriented programming. One of the advantages of object-oriented programming is the reusability of code, which is made possible by the relationship or interaction between classes to achieve different features or implementations of an application.

There are four types of relationships in object-oriented programming based on how a class interacts with another class: association, inheritance, composition, and aggregation. All these types of interaction are based on “is a” relationship, “has-a” relationship, and “uses-a” relationship.

Association(“uses-a” type of relationship)

An association relationship is called a “uses a” relationship, where one class uses another class to perform a specific action. It is a relationship between two objects. In other words, it defines the association between multiple objects. It can also be seen as collaboration or delegation.

Association happens between the classes occurs when a class provides a service to another class or the class delegates some kind of behavior to another class. The relationship can be one-to-one, one-to-many, many-to-one, or many-to-many.

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 = "Alex Onah";
Employee Emp_Obj = new Employee();
Emp_Obj.Emp_Name = "David";
Emp_Obj.Manager_Name(Man_Obj);
ReadLine();
}
}

Output

Out
Manager of Employee David is Alex Onah

The above example shows an association relationship because both Employee and Manager classes using the object of each other and both their own independent life cycle.

Inheritance(“is a” type of relationship)

Inheritance is an “IS-A” type of relationship. The “IS-A” type of relationship is based on Inheritance. Inheritance allows you to build a class that inherits the members of its parent or base class.

In this type of relationship, we can create a new class by using codes from an existing class. It is just like saying that “A is a type of B”. For example “Jollof Rice is a food”, “Mango is a fruit” etc.

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }

public string GetFullName(){
return FirstName + " " + LastName;
}
}

class Employee : Person
{
public int EmployeeId { get; set; }
public string CompanyName { get; set; }

}

Composition and Aggregation(“has a” type of relationship)

Composition(“part of” type of relationship)

Composition is referred to as “has a” relationship. A composition relationship is formed when a class has a reference to another class as an instance property. It is also known as a “part-of” relationship.

It is another important relationship type in object-oriented programming that exists when an object from one class, is made up of or composed of one or more objects from another class

image by c-sharp-corner
using System;  
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;

namespace Composition
{
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} \n Maximum Speed is {this.Max_Speed}\n Numbers of Seets is\n {this.Total_Seats} Model_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

Out
Color Of car is Black
Maximum Speed is 240KM/Hour
Numbers of Seats is 4
Model_No is SUZ234
Engine is 4 stroke and fuel effisiency is good
_

Aggregation

Aggregation is another form of “has a” relationship where a class can contain other classes as properties but those classes can exist independently.

It is a special form of association. In association, there are not any classes working as the owner but in aggregation one class work as an owner. In aggregation, both entities meet for some work and then get separated. Aggregation is a one-way association.

public class Student
{
public int StudentId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }

public Course EnrolledCourse { get; set; }
}

public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public IList Topics { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}

Supposing In the aggregation relationship above, even if the Student object is deleted, the Course object will still exist. The Student class can also contain CourseId property instead of Course instance.

  • A class (parent) contains a reference to another class (child) where both classes can exist independently.
  • A class can also include a reference of the id property of another class.

--

--