Welcome to our next lesson. In our previous session, we focused on defining the internal structure of a single class using fields, properties, and access modifiers to achieve robust encapsulation. Now, we'll expand our view to see how classes can interact and build upon one another.
Today, we will cover three fundamental concepts of object-oriented programming in C#: inheritance, method overriding, and method overloading. These are the primary mechanisms for creating hierarchies of related classes, promoting code reuse, and enabling polymorphism—the ability for objects of different types to respond to the same method call in unique ways. Mastering these concepts is not only essential for writing effective C# code but also a core expectation in technical interviews.
1. Inheritance: Creating "is-a" Relationships
Inheritance is the mechanism by which one class, the derived or child class, can acquire the properties and methods of another class, the base or parent class. This creates an "is-a" relationship. For example, a Dog "is-a" type of Animal. The derived class inherits all public and protected members from its base class, allowing for significant code reuse.
The basic syntax in C# is straightforward:
// Base class
public class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}
// Derived class
public class Dog : Animal // The ':' denotes inheritance
{
public void Bark()
{
Console.WriteLine($"{Name} says: Woof!");
}
}
In this example, an instance of Dog has access to its own Bark() method as well as the inherited Name property and Eat() method from the Animal class.
2. Method Overriding: Specializing Inherited Behavior
While inheritance is great for reuse, you'll often find that a derived class needs to provide its own specific implementation of an inherited method. This is called method overriding. For instance, while all animals might make a sound, a dog barks and a cat meows.
To enable method overriding in C#, you must use a pair of keywords:
virtual: You add this keyword to a method in the base class to indicate that it can be overridden by a derived class.override: You use this keyword in the derived class to provide the new implementation for that virtual method.
This process is a key part of achieving runtime polymorphism, because the exact method that gets executed is determined at runtime based on the actual type of the object.

The following reading provides clear definitions and code examples for both overriding and the essential keywords involved.
Method Overloading and Method Overriding in C# - ScholarHat
This resource from Scholarhat offers a detailed walkthrough of method overriding, including the roles of the virtual, override, and base keywords.
First, read the introduction Method Overriding in C# to get the formal definition. Next, focus on the "Types of Keywords for Method Overriding in C#". Read the definitions and study the code examples for each of the three keywords: Virtual Keyword: Look at the Animal, Dog, and Cat example to see how a base method is marked virtual. Override Keyword: Review the Shape, Circle, and Square example which reinforces the virtual/override relationship. Base Keyword: Examine the web and stream example. Pay close attention to how base.showdata() is used within the overridden method to call the parent's implementation. This is a very common and useful pattern.
The base keyword is particularly powerful. It allows you to extend the functionality of a base class method rather than completely replacing it. You can execute the base class logic and then add more specific logic from the derived class.
3. Method Overloading: Same Name, Different Signatures
Method overloading is a different concept that occurs within a single class. It allows you to define multiple methods with the same name, as long as they have different signatures. A method's signature is defined by its name and its parameter list (the number, type, and order of its parameters). The return type is not part of the signature for overloading purposes.
This is a form of compile-time polymorphism because the compiler can determine which version of the method to call at compile time based on the arguments you provide. For example, you might have a Calculate method that can accept two integers, three integers, or two doubles.
Method Overloading and Method Overriding in C# - ScholarHat
This part of the same Scholarhat resource provides a concise explanation of method overloading with clear, practical examples.
Start with the definition section, Method overloading in C#. Then, review the three code examples that show the different ways a method can be overloaded: By changing the number of parameters. By changing the data types of parameters. By changing the order of parameters.
Overloading is useful for creating a more intuitive and flexible API for your class, where related operations can share a single, memorable name.
4. Overriding vs. Overloading: The Classic Interview Question
Being able to clearly articulate the difference between method overriding and overloading is a standard OOP interview question. The following video and summary table will help solidify your understanding.
Watch this quick video for a direct comparison focused on what you'd need to explain in an interview.
Overloading vs Overriding in C# .NET
This video from Interview Happy provides a rapid but clear comparison between overloading and overriding, perfect for interview prep.
The video first recaps method overriding with its key characteristics. Then, it presents a direct point-by-point comparison with overloading. Focus on this comparative segment.
Here is a summary of the key differences, as shown in the provided table resource.

To summarize:
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Scope | Occurs within the same class. | Occurs between a base and derived class. |
| Method Signature | Methods must have the same name but different signatures (i.e., different parameters). | Methods must have the same name and the same signature. |
| Purpose | To add more behavior or functionality to a class. | To change the existing behavior of an inherited method. |
| Polymorphism | Implements compile-time polymorphism (static binding). | Implements run-time polymorphism (dynamic binding). |
| Keywords | No special keywords required. | Requires virtual in the base class and override in the derived class. |
5. Best Practices and Common Pitfalls
Demonstrating that you not only know the definitions but also understand how to apply these concepts cleanly shows a higher level of proficiency.
Clean Coding with Method Overriding and Overloading in C#
This resource from CodeSignal outlines best practices and common problems, helping you avoid mistakes and write cleaner code.
First, read the list of Best Practices. The point about considering composition before defaulting to inheritance is an advanced topic that shows great design awareness. Next, review the Bad Example and the corresponding Refactored Example. Notice how in the "bad" code, a method like DoTask(double a) in the Child class doesn't override DoTask(int a) from the Parent; it overloads it. This is a subtle but critical bug that the override keyword helps prevent by causing a compiler error if the signatures don't match.
Conclusion
In this lesson, you've learned about the fundamental OOP mechanisms that allow you to build complex and flexible class hierarchies in C#. These concepts are the bedrock of reusable and maintainable object-oriented design.
The key takeaways are:
- Inheritance creates "is-a" relationships, allowing a derived class to reuse code from a base class.
- Method Overriding allows a derived class to provide a specialized implementation for an inherited method using the
virtualandoverridekeywords. This is an example of runtime polymorphism. - Method Overloading provides multiple versions of a method within the same class, differentiated by their parameter lists. This is an example of compile-time polymorphism.
- The
basekeyword is used in a derived class to access members (like methods) of its base class, which is useful for extending, rather than completely replacing, inherited behavior.
Our next lesson will build directly on these ideas. We will explore abstract classes and interfaces, which take the concepts of inheritance and polymorphism a step further by allowing you to define contracts that multiple classes must adhere to, a critical technique for building large-scale, loosely-coupled applications.