Skip to main content

12. Inheritance in C#

Level: Beginner

ℹ️ What You'll Learn
  • What inheritance is
  • Base class and derived class
  • Reuse common properties
  • Use base constructor
  • Use virtual and override
  • 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
  • Email
  • 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 virtual in parent = "child can override this"
  • Use override in 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

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
Student student = new Student("Sahasra Kumar", 101, "10-A");
student.Email = "Sahasra@school.com";
student.PrintInfo();

Teacher teacher = new Teacher("Meena Rao", "EMP501", "Mathematics");
teacher.Email = "meena@school.com";
teacher.PrintInfo();

public class Person
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";

public Person(string name)
{
Name = name;
}

public virtual void PrintInfo()
{
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Email: {Email}");
}
}

public class Student : Person
{
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";

public Student(string name, int rollNumber, string className) : base(name)
{
RollNumber = rollNumber;
ClassName = className;
}

public override void PrintInfo()
{
Console.WriteLine("=== Student ===");
base.PrintInfo();
Console.WriteLine($"Roll Number: {RollNumber}");
Console.WriteLine($"Class: {ClassName}");
Console.WriteLine();
}
}

public class Teacher : Person
{
public string EmployeeCode { get; set; } = "";
public string Subject { get; set; } = "";

public Teacher(string name, string employeeCode, string subject) : base(name)
{
EmployeeCode = employeeCode;
Subject = subject;
}

public override void PrintInfo()
{
Console.WriteLine("=== Teacher ===");
base.PrintInfo();
Console.WriteLine($"Employee Code: {EmployeeCode}");
Console.WriteLine($"Subject: {Subject}");
Console.WriteLine();
}
}

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, use Student.PrintInfo()
  • If actual object is Teacher, use Teacher.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
  • Email
  • 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 Person
  • person.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:

  1. Change method to PrintRole() instead of PrintInfo()
  2. Override PrintRole() in both Student and Teacher
  3. Test: both classes should show their own role

See how polymorphism lets same method name work differently for each class.


ℹ️ Video Tutorial

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

  1. Use inheritance for true "is-a" relationship.
  2. Put only common code in base class.
  3. Keep inheritance simple.
  4. Use virtual only when child should change behavior.
  5. Use base.MethodName() when child wants parent behavior plus extra behavior.

Practice Task

Create:

  • Person class with Name and Phone
  • Parent class that inherits Person
  • Teacher class that inherits Person
  • Override PrintInfo() in both child classes

Quick Revision

QuestionAnswer
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

🎯 Q1: What is inheritance?

Inheritance allows one class to reuse properties and methods from another class.

🎯 Q2: What is base class and derived class?

Base class is the parent class. Derived class is the child class that inherits from the parent.

🎯 Q3: What is virtual and override?

virtual in parent means child can change the method. override in child means child provides its own version.

🎯 Q4: What is base keyword?

base is used to call parent constructor or parent method from the child class.

🎯 Q5: What is polymorphism?

Polymorphism means the same method call can behave differently depending on the actual object type.


🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Abstract Classes ->

nexcoding.in