OOP: C# (.NET) में इनहेरिटेंस: एक विस्तृत गाइड
What is it and why? (a real-life analogy)
Imagine you decided to create your own video game. In this game, there are different types of vehicles: cars, motorcycles, trucks, and bicycles. Each of them has something in common: they can all move, they all have wheels, they can all honk. But at the same time, each type of vehicle does it in its own way. A car drives on the road, a truck carries heavy loads, and a bicycle can ride on paths.
If you were writing code for each type of vehicle from scratch, you would have to describe what wheels are, how the engine works, and how to turn on the headlights every single time. This would be very long and tedious. And if you wanted to add a new feature, like "turn on the air conditioner," you would have to change the code in all the classes at once.
Inheritance is a mechanism in object-oriented programming that allows you to create one common class (let's call it "Vehicle"), which describes all the common properties and methods. Then, for each specific type of vehicle, you create your own class that inherits everything from the common class, but can also add its own features or change the parent's behavior.
Imagine you are building a house. The foundation, walls, and roof are the common "House" class. Then you build specific houses: "Cottage with a pool," "City apartment," "Country house." Each of them uses common elements but adds its own unique details. Inheritance in programming works exactly the same way.
Why is this needed? Firstly, it saves time — you don't write the same code a hundred times. Secondly, it simplifies maintenance — if you need to change something common, you only change it in one place. Thirdly, it makes the code more logical and understandable — you see the class hierarchy and understand how they are related to each other.
Theory: how it works internally
Let's look "under the hood" and understand what actually happens in the computer's memory when we use inheritance.
Base and derived classes
In inheritance, there are always two participants:
- Base class (parent, superclass) — this is the class from which others inherit. It contains common fields, properties, and methods.
- Derived class (child, subclass) — this is the class that inherits from the base class. It gets everything the parent has and can add its own.
How C# stores objects in memory
When you create an object of a derived class, memory is allocated for all fields — both those declared in the derived class and those inherited from the base class. Imagine a Russian nesting doll: the largest doll is the base class, and inside it is a smaller doll, the derived class. When you access a field or method, the compiler first looks for it in the derived class itself. If it doesn't find it, it goes up one level to the base class.
Access modifiers and inheritance
Not everything the parent has is accessible to the child. In C#, there are special keywords that control visibility:
| Modifier | Accessible in the derived class? | Accessible from outside? |
|---|---|---|
public |
Yes | Yes |
private |
No | No |
protected |
Yes | No |
internal |
Yes (within the assembly) | Yes (within the assembly) |
protected internal |
Yes | Yes (within the assembly) |
private protected |
Yes (only within the assembly) | No |
Important: private fields and methods of the parent are not inherited. They exist in the object, but the child class cannot access them directly. This is done to protect data — the parent can hide its internal implementation details.
Virtual methods and overriding
Sometimes it happens like this: the parent has a method, but the child wants to do it its own way. For this, C# uses the virtual methods mechanism. The parent marks the method with the keyword virtual, and the child marks it with override. This means: "Hey, I know dad does it this way, but I will do it differently."
When you call such a method on an object, C# looks not at the type of the variable, but at the actual type of the object in memory. This is called polymorphism — one of the three pillars of OOP (along with inheritance and encapsulation).
Constructors and inheritance
When an object of a derived class is created, the base class constructor is called first, and then the derived class constructor. This is logical: first you need to build the foundation (the parent), and then you can build the floors (the child).
If the base class does not have a parameterless constructor, the derived class must explicitly call one of the parent's constructors using the base keyword.
Syntax in C# (.NET): all forms and variations
Let's break down the syntax of inheritance in C# from simple to complex.
1. Simple inheritance
In C#, a class can inherit from only one base class. This is called single inheritance.
// Base class
public class Animal
{
public string Name