Object-Oriented Programming (OOP) in C# provides powerful tools like inheritance and polymorphism to create structured, reusable, and flexible code. This guide explores hands-on implementation techniques, including base/derived class creation, method overriding, and interface usage.
1. Inheritance in C#: Building Class Hierarchies
Inheritance allows a derived class to inherit properties and methods from a base class, reducing redundancy and improving code organization.
Defining a Base Class
A base class serves as a template for derived classes. Key steps:
- Use an access modifier (public, private, protected)
- Define properties (data) and methods (actions)
Example: Pool Base Class
public class Pool
{
public int ChlorineLevel { get; set; }
public int WaterLevel { get; set; }
public Pool(int chlorine, int water)
{
ChlorineLevel = chlorine;
WaterLevel = water;
}
public void DisplayInfo()
{
Console.WriteLine($"Pool - Chlorine: {ChlorineLevel}, Water: {WaterLevel}");
}
}
Creating a Derived Class
A derived class extends the base class using : BaseClassName syntax.
Example: Spa Derived Class
public class Spa : Pool
{
public int HeatLevel { get; set; }
public Spa(int chlorine, int water, int heat)
: base(chlorine, water) // Calls base class constructor
{
HeatLevel = heat;
}
public void DisplaySpaInfo()
{
Console.WriteLine($"Spa - Chlorine: {ChlorineLevel}, Water: {WaterLevel}, Heat: {HeatLevel}");
}
}
Key Benefits:
✔ Reusability – Inherits Pool properties/methods
✔ Extensibility – Adds new features (HeatLevel)
2. Polymorphism in C#: Flexible Method Implementations
Polymorphism allows methods to behave differently based on the object’s type. Implemented via:
- Method overriding (using virtual & override)
- Interfaces (contract-based implementation)
Method Overriding
- Base Class: Declare a method with virtual
- Derived Class: Redefine it with override
Example: Instrument Hierarchy
public class Instrument
{
public virtual void Play()
{
Console.WriteLine("Playing a generic instrument");
}
}
public class Piano : Instrument
{
public override void Play()
{
Console.WriteLine("Playing the piano: Melodic keys!");
}
}
Usage:
Instrument myInstrument = new Piano();
myInstrument.Play(); // Output: "Playing the piano: Melodic keys!"
Interfaces for Polymorphism
Interfaces enforce method implementation without inheritance.
Example: IPlayable Interface
public interface IPlayable
{
void Play();
}
public class Guitar : IPlayable
{
public void Play()
{
Console.WriteLine("Strumming the guitar!");
}
}
public class Drums : IPlayable
{
public void Play()
{
Console.WriteLine("Beating the drums!");
}
}
Usage:
List<IPlayable> band = new List<IPlayable> { new Guitar(), new Drums() };
foreach (var instrument in band)
{
instrument.Play(); // Calls respective implementations
}
3. Real-World Application: Pool Management System
Combining inheritance and polymorphism for modular design:
// Base class
public class SwimmingFacility
{
public string Location { get; set; }
public virtual void Maintain()
{
Console.WriteLine("Performing standard maintenance");
}
}
// Derived class
public class OlympicPool : SwimmingFacility
{
public int LaneCount { get; set; }
public override void Maintain()
{
base.Maintain(); // Reuse base method
Console.WriteLine($"Cleaning {LaneCount} lanes");
}
}
// Interface implementation
public interface IMaintainable
{
void CheckSafety();
}
public class KiddiePool : SwimmingFacility, IMaintainable
{
public void CheckSafety()
{
Console.WriteLine("Inspecting child safety features");
}
}
Conclusion: Best Practices for OOP in C#
- Inheritance
- Use for “is-a” relationships (e.g., Spa is a Pool)
- Avoid deep hierarchies (favor composition over inheritance)
- Polymorphism
- Prefer interfaces for multiple behavior contracts
- Use virtual/override for controlled method extensions
- Design Tips
- Encapsulate fields with properties ({ get; set; })
- Use abstract classes for partial implementations
- Apply SOLID principles for maintainable code
By mastering these techniques, you can build modular, scalable, and maintainable C# applications that leverage the full power of OOP.