Understanding “Is-A” and “Has-A” Relationships in C#
Every class in your C# project lives in one of two relationships with others — it either “is something” or “has something.”
If that sounds philosophical, don’t worry — by the end of this read, you’ll start recognizing these patterns everywhere: in your favorite apps, your car, even your phone.
The “Is-A” Relationship — When One Object Belongs to a Family
The “Is-A” relationship is all about inheritance. It’s when one class inherits the identity and behavior of another.
In simple words, when you say “an ElectricCar is a Vehicle”, you’re saying ElectricCar belongs to the family of Vehicles — it shares what every Vehicle can do (like StartEngine() or Stop()), but may perform it differently.
Let’s see how that looks in C#.
public class Vehicle
{
public string Brand { get; set; }
public void StartEngine()
{
Console.WriteLine(”Engine started with fuel.”);
}
}
public class ElectricCar : Vehicle
{
public int BatteryLevel { get; set; }
public void Charge()
{
Console.WriteLine(”Charging the battery...”);
}
public new void StartEngine()
{
Console.WriteLine(”Engine started silently using electricity.”);
}
}
Now when you create an object of ElectricCar, it can do everything a Vehicle does — plus some electric-specific things.
In your mind, think:
A
Vehicleis the parent, anElectricCaris the child that grows up and learns new tricks.
You can use it like this:
ElectricCar tesla = new ElectricCar();
tesla.Brand = “Tesla”;
tesla.StartEngine(); // ⚡ Engine started silently using electricity.
tesla.Charge(); // 🔋 Charging the battery...
The child inherited the parent’s DNA and extended it — that’s what “Is-A” means.
In the real world, you’ll use this whenever your classes share a common behavior but differ in execution.
When to Use “Is-A”
You use “Is-A” when you want:
To group multiple entities under a common base (like
Employee,Manager,Intern).To reuse logic (shared properties like
Name,Id,Department).To override or customize behavior (like different ways of
CalculateSalary()).
But remember: don’t use inheritance just to access another class’s code. That’s where “Has-A” comes in.
The “Has-A” Relationship — When One Object Owns or Uses Another
The “Has-A” relationship means one class contains or uses another.
It’s not family — it’s collaboration.
Imagine your car again. A car is a vehicle (inheritance), but it has an engine (composition).
The engine isn’t a type of car — it’s a part that the car depends on.
Here’s what that looks like in code:
public class Engine
{
public void Ignite()
{
Console.WriteLine(”Engine ignited!”);
}
}
public class Car
{
private Engine _engine;
public Car()
{
_engine = new Engine(); // The car “has” an engine
}
public void Start()
{
Console.WriteLine(”Starting the car...”);
_engine.Ignite();
Console.WriteLine(”Car is now running.”);
}
}
Now, when you start the car:
Car myCar = new Car();
myCar.Start();
You’ll see:
Starting the car...
Engine ignited!
Car is now running.
The car doesn’t inherit from Engine — it simply owns one.
The “Has-A” relationship lets you compose classes by combining functionality rather than sharing DNA.
The Real-World Difference
Let’s use a more relatable example from the tech world — your smartphone.
Your phone is a Device. (It inherits common behavior like powering on/off.)
Your phone has a Camera. (It uses the camera but doesn’t inherit from it.)
You wouldn’t say:
“A Phone is a Camera.”
That wouldn’t make sense — the camera is just one of the components your phone relies on.
In C#, that’s exactly how you decide whether to use inheritance or composition:
If one object is a specialized version of another → use inheritance.
If one object uses or contains another → use composition.
Putting It All Together — Food Delivery Example
Let’s combine both “Is-A” and “Has-A” in a real, connected scenario.
Imagine you’re designing an online food delivery system.
We have:
A
DeliveryPersonwho is anEmployee.A
DeliveryAppthat has aPaymentGatewayand has aDeliveryPerson.
Let’s see how that plays out:
// IS-A relationship
public class Employee
{
public string Name { get; set; }
}
public class DeliveryPerson : Employee
{
public void DeliverOrder()
{
Console.WriteLine(”Delivering the order...”);
}
}
// HAS-A relationship
public class PaymentGateway
{
public void ProcessPayment()
{
Console.WriteLine(”Payment processed successfully!”);
}
}
public class DeliveryApp
{
private DeliveryPerson _deliveryPerson;
private PaymentGateway _paymentGateway;
public DeliveryApp()
{
_deliveryPerson = new DeliveryPerson();
_paymentGateway = new PaymentGateway();
}
public void CompleteOrder()
{
Console.WriteLine(”🧾 Processing order...”);
_paymentGateway.ProcessPayment();
_deliveryPerson.DeliverOrder();
Console.WriteLine(”Order completed successfully!”);
}
}
Now, when the app processes an order:
DeliveryApp app = new DeliveryApp();
app.CompleteOrder();
You’ll see:
Processing order...
Payment processed successfully!
Delivering the order...
Order completed successfully!
The relationships are clear:
DeliveryPerson IS-A EmployeeDeliveryApp HAS-A PaymentGatewayDeliveryApp HAS-A DeliveryPerson
Each class plays its own role without unnecessary coupling.
Final Thoughts
Understanding “Is-A” and “Has-A” is like knowing when to inherit traits and when to collaborate in life.
Your team at work might share a company culture (inheritance), but each person has their own laptop, phone, or tools (composition).
In programming:
“Is-A” builds hierarchies (Vehicle → ElectricCar).
“Has-A” builds systems (Car → Engine).
Great design balances both.
If you master when to extend and when to include, you’ll write software that feels natural, flexible, and future-proof.


