Please mujy c# me inheritance or polymorphism bata de
Loading
Please mujy c# me inheritance or polymorphism bata de
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sudarshan HajarePosted Jul 9, 2026, 4:58 AM
Inheritance — "borrowing from a parent"
Inheritance means one class can reuse the properties and methods of another class, instead of writing everything from scratch.
Think of it like this: a
Carand aBikeare bothVehicles. They both have a speed, both can start, both can stop. Instead of writing that logic twice, you write it once in aVehicleclass, and letCarandBike"inherit" it.Now
Managerautomatically getsName,BaseSalary, andShowDetails()— you didn't write them again. You just added the extra thing a Manager has (TeamBonus).Polymorphism — "same call, different behavior"
Polymorphism means you can call the same method name on different objects, and each one behaves in its own way — the calling code doesn't need to know which exact type it's dealing with.
Now watch this:
Output:
Same line of code —
person.ShowDetails()— but it behaves differently depending on whether the actual object is aManageror aDeveloper. That's polymorphism — "many forms" of the same action.The simplest way to remember the difference
Cynthia SathuragiriPosted Feb 19, 2026, 4:55 AM
Inheritance
Inheritance allows one class to inherit properties and methods from another class.
It helps with:
Code reuse
Cleaner structure
Reducing duplication
class Parent
{
public void Show()
{
Console.WriteLine("Parent class method");
}
}
class Child : Parent
{
public void Display()
{
Console.WriteLine("Child class method");
}
}
Child obj = new Child();
obj.Show(); // Inherited from Parent
obj.Display(); // Child’s own method
Child : Parent? Child inherits Parent.Child automatically gets access to Parent’s public methods.
Polymorphism
Polymorphism means "many forms."
A method can behave differently depending on how it is used.
In C#, there are two main types:
Compile-time Polymorphism (Method Overloading)
Run-time Polymorphism (Method Overriding)
Method Overloading (Compile-Time)
Same method name, but different parameters.
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
The method name is the same (
Add)Parameters are different
The compiler decides which one to call
Method Overriding (Run-Time)
A child class changes the behavior of a parent class method.
class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal makes sound");
}
}
class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog barks");
}
}
Animal obj = new Dog();
obj.Sound();
Output:
Dog barks
virtual? Allows the method to be overriddenoverride? Replaces the base methodThe decision happens at runtime