Inheritance
Basics of Inheritance
Example
using System;
// Base class
class Person
{
 // Fields
 private string firstName;
 private string lastName;
 private int age;
  // Constructor
 public Person(string firstName, string lastName, int age)
 {
   this.firstName = firstName;
   this.lastName = lastName;
   this.age = age;
 }
  // Properties
 public string FirstName
 {
   get { return firstName; }
   set { firstName = value; }
 }
  public string LastName
 {
   get { return lastName; }
   set { lastName = value; }
 }
  public int Age
 {
   get { return age; }
   set
   {
     if (value >= 0)
       age = value;
     else
       throw new ArgumentOutOfRangeException("Age must be a positive number.");
   }
 }
  // Method
 public void DisplayDetails()
 {
   Console.WriteLine($"Name: {FirstName} {LastName}, Age: {Age}");
 }
  // Virtual method for overriding
 public virtual void PrintRole()
 {
   Console.WriteLine("Person's role: Unknown");
 }
}
// Derived class
class Student : Person
{
 // Additional property
 public string Major { get; set; }
  // Constructor
 public Student(string firstName, string lastName, int age, string major)
   : base(firstName, lastName, age)
 {
   Major = major;
 }
  // Method overriding
 public override void PrintRole()
 {
   Console.WriteLine("Person's role: Student");
 }
  // Method to display student details
 public void DisplayStudentDetails()
 {
   DisplayDetails();
   Console.WriteLine($"Major: {Major}");
 }
}
// Derived class
class Teacher : Person
{
 // Additional property
 public string Subject { get; set; }
  // Constructor
 public Teacher(string firstName, string lastName, int age, string subject)
   : base(firstName, lastName, age)
 {
   Subject = subject;
 }
  // Method overriding
 public override void PrintRole()
 {
   Console.WriteLine("Person's role: Teacher");
 }
  // Method to display teacher details
 public void DisplayTeacherDetails()
 {
   DisplayDetails();
   Console.WriteLine($"Subject: {Subject}");
 }
}
class Program
{
 static void Main()
 {
   // Creating an object of the Student class
   Student student = new Student("Priyanka", "Tiwari", 21, "Computer Science");
   student.DisplayStudentDetails();
   student.PrintRole();
    // Creating an object of the Teacher class
   Teacher teacher = new Teacher("Bob", "Smith", 46, "Mathematics");
   teacher.DisplayTeacherDetails();
   teacher.PrintRole();
 }
}
Explanation of the Example
Base Class (Person): The Person class contains fields for ‘firstName’, ‘lastName’, and ‘age’, along with properties to access and modify these fields. It has a constructor that initializes these fields and a method called ‘DisplayDetails’ to print the person’s details. Additionally, the ‘PrintRole’ method is declared as virtual, enabling derived classes to override it.
Derived Class (Student): The Student class inherits from Person, using the ‘:’ symbol. It includes an additional property called ‘Major’. The constructor of the Student class calls the base class constructor to initialize the inherited fields and sets the Major property. It also overrides the ‘PrintRole’ method to specify the role as “Student”. The ‘DisplayStudentDetails’ method invokes the base class’s ‘DisplayDetails’ method and prints the major.
Derived Class (Teacher): The Teacher class, like Student, inherits from Person and adds an additional property called ‘Subject’. Its constructor calls the base class constructor to initialize inherited fields and sets the Subject property. The ‘PrintRole’ method is overridden to indicate that the role is “Teacher”. The ‘DisplayTeacherDetails’ method calls the base class’s ‘DisplayDetails’ method and prints the subject.
Main Method:Â In the main method, we create an instance of Student and call its methods to display the student’s details and role. We also create an instance of Teacher and call its methods to display the teacher’s details and role.
Key Concepts
- Inheritance: The Student and Teacher classes inherit from the Person class, reusing its fields, properties, and methods.
- Constructor Chaining: Derived class constructors call the base class constructor using the base keyword to initialize inherited fields.
- Method Overriding:Â The PrintRole method is overridden in the derived classes to provide specific behavior for students and teachers.
- Encapsulation:Â Fields in the base class are encapsulated using properties, ensuring controlled access and validation.
