12. Inheritance in C#
Level: Beginner
- What inheritance is
- Base class and derived class
- Reuse common properties
- Use
baseconstructor - Use
virtualandoverride - Understand polymorphism in simple words
In the previous article, you learned classes and objects.
Now imagine this:
- Student has Name, Email, Phone
- Teacher has Name, Email, Phone
- Admin staff has Name, Email, Phone
If we copy these properties into every class, code is repeated.
Inheritance solves this.
The Problem: Repeated Common Data
Without inheritance:
public class Student
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
}
public class Teacher
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
}
Name and Email are repeated.
Better:
Person
|-- Student
|-- Teacher
Put common data in Person.
What is Inheritance?
Inheritance means one class gets properties and methods from another class.
Base class = Parent class
Derived class = Child class
Example:
public class Student : Person
{
}
Student : Person means:
Student is a Person.
Student gets Person properties and methods.
Quick Definitions
- Base class - Parent class with common properties and methods
- Derived class - Child class that inherits from base class
- Inheritance - Child class gets parent's properties and methods
- Virtual - Marks method that child can override
- Override - Child provides its own version of parent method
- Polymorphism - Same method name, different behavior in different classes
- Reuse - Using parent code in child classes (avoid repeating)
- Parent - Another name for base class
- Child - Another name for derived class
Step 1: Create Base Class
public class Person
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public string Phone { get; set; } = "";
public void PrintBasicInfo()
{
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Email: {Email}");
Console.WriteLine($"Phone: {Phone}");
}
}
Person has common data.
Step 2: Create Derived Class
public class Student : Person
{
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";
public double Percentage { get; set; }
}
Student automatically gets:
- Name
- Phone
- PrintBasicInfo()
And adds:
- RollNumber
- ClassName
- Percentage
Step 3: Use Inherited Properties
Student student = new Student();
student.Name = "Sahasra Kumar";
student.Email = "Sahasra@school.com";
student.Phone = "9876543210";
student.RollNumber = 101;
student.ClassName = "10-A";
student.Percentage = 84.8;
student.PrintBasicInfo();
Name, Email, and Phone came from Person.
Step 4: Teacher Also Inherits Person
public class Teacher : Person
{
public string EmployeeCode { get; set; } = "";
public string Subject { get; set; } = "";
}
Now both Student and Teacher reuse common Person code.
Step 5: Constructor with base
When child is created, parent constructor must also run.
Example: When creating a Student, parent Person constructor sets the Name. Student constructor sets RollNumber.
Use base(...) to call parent constructor.
Syntax:
public class Person
{
public string Name { get; set; } = "";
public Person(string name)
{
Name = name;
}
}
public class Student : Person
{
public int RollNumber { get; set; }
public Student(string name, int rollNumber) : base(name)
{
RollNumber = rollNumber;
}
}
Usage:
Student student = new Student("Sahasra Kumar", 101);
Step 6: Why Child Methods Are Different (virtual and override)
Sometimes parent has a method, but child needs its own version.
Example:
Person's PrintRole() shows "I'm a school member."
Student's PrintRole() should show "I'm a student."
Teacher's PrintRole() should show "I'm a teacher."
Same method name. Different behavior.
To allow child to change parent method:
- Use
virtualin parent = "child can override this" - Use
overridein child = "I'm providing my own version"
Step 7: How virtual and override Work
Use virtual in parent.
Use override in child.
public class Person
{
public string Name { get; set; } = "";
public virtual void PrintRole()
{
Console.WriteLine("School member");
}
}
public class Student : Person
{
public override void PrintRole()
{
Console.WriteLine("Student");
}
}
Full Example: Person, Student, Teacher
What is Polymorphism?
Polymorphism means one method name can work in different ways.
In our example, PrintInfo() is the same method name. But Student and Teacher can print different details.
Real example:
Person person1 = new Student("Sahasra", 101, "10-A");
Person person2 = new Teacher("Meena", "EMP501", "Maths");
person1.PrintInfo(); // Prints Student details
person2.PrintInfo(); // Prints Teacher details
Key insight: Both are Person type, but method behavior depends on actual object.
- If actual object is
Student, useStudent.PrintInfo() - If actual object is
Teacher, useTeacher.PrintInfo()
This is polymorphism: the same method call runs the correct version based on the real object.
Why it matters: You can keep different school members in one Person list. When you call PrintInfo(), C# knows whether the item is a Student or Teacher.
When You'll Use This in SMS
Inheritance organizes school entities into a hierarchy.
Class hierarchy:
Person (base)
|-- Student (roll number, class, percentage)
|-- Teacher (employee code, subject)
+-- Staff (designation, department)
Shared Person data:
- Name
- Phone
When you add a field to Person (like Address), all child classes automatically get it.
Real impact:
- Add field once in
Person= all children inherit it - Override method in
Student= students have their own behavior - Polymorphism = call same method on Student/Teacher/Staff, each behaves correctly
Example advantage:
Person person = new Student(...)- works because Student IS-A Personperson.PrintInfo()- calls Student version automatically
Production SMS has inheritance everywhere. Change common field = all entities updated automatically.
Try This Now
Run the full example above. Then experiment:
- Change method to
PrintRole()instead ofPrintInfo() - Override
PrintRole()in both Student and Teacher - Test: both classes should show their own role
See how polymorphism lets same method name work differently for each class.
Inheritance explained: base class, derived class, virtual, override, polymorphism. Video coming soon. Subscribe to NexCoding YouTube for updates.
Common Mistakes
Mistake 1: Forgetting : Person
Without inheritance, child class does not get parent properties.
Mistake 2: Using override without virtual
Parent method must be marked virtual before child can override.
Mistake 3: Forgetting base constructor
If parent constructor needs data, child must call it using base(...).
Mistake 4: Using inheritance for wrong relationship
Use inheritance only when the sentence makes sense:
Student is a Person -> good
Teacher is a Person -> good
Student is a FeeAccount -> wrong
Best Practices
- Use inheritance for true "is-a" relationship.
- Put only common code in base class.
- Keep inheritance simple.
- Use
virtualonly when child should change behavior. - Use
base.MethodName()when child wants parent behavior plus extra behavior.
Practice Task
Create:
Personclass withNameandPhoneParentclass that inheritsPersonTeacherclass that inheritsPerson- Override
PrintInfo()in both child classes
Quick Revision
| Question | Answer |
|---|---|
| What is inheritance? | Reusing parent class code |
| What is base class? | Parent class |
| What is derived class? | Child class |
What does : Person mean? | Inherits from Person |
What does base do? | Calls parent constructor/method |
What does override do? | Changes parent virtual method |
Inheritance allows one class to reuse properties and methods from another class.
Base class is the parent class. Derived class is the child class that inherits from the parent.
virtual in parent means child can change the method. override in child means child provides its own version.
base is used to call parent constructor or parent method from the child class.
Polymorphism means the same method call can behave differently depending on the actual object type.
Use ChatGPT, Claude, or Copilot to go deeper on C# inheritance. Try these prompts:
"Explain inheritance using Student and Teacher examples""Explain virtual, override, and base simply""Give me 5 practice tasks for inheritance""Quiz me with 5 beginner questions about inheritance"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.