Inheritance
1. Description
Inheritance is a fundamental principle of Object-Oriented Programming (OOP) that allows a class to inherit properties and methods from another class. The class that is being inherited from is called the base class (or superclass), and the class that inherits is called the derived class (or subclass).
The derived class can reuse the code from the base class, extend it with new functionality, and modify its behavior (through method overriding).
2. Why It Is Important
Inheritance is a powerful mechanism for code reuse and for establishing relationships between classes. It allows you to:
- Avoid Code Duplication: Common functionality can be placed in a base class and reused across multiple derived classes.
- Create a Logical Hierarchy: You can model "is-a" relationships (e.g., a
Dogis anAnimal, aStudentis aPerson). - Enable Polymorphism: Inheritance is a prerequisite for polymorphism, which allows you to treat objects of different derived classes as objects of the base class.
3. Real-World Examples
- A
Vehiclebase class can have properties likeSpeedandColor. Derived classes likeCar,Truck, andMotorcyclecan inherit these properties and add their own specific ones (e.g.,NumberOfDoorsfor aCar). - In a UI framework, you might have a base
Controlclass with properties likePositionandSize, and derived classes likeButton,TextBox, andCheckbox. - A
Personclass can be a base class forStudent,Teacher, andAdministratorclasses in a university management system.
4. Syntax & Explanation
using System;
// Base class
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
public void Greet()
{
Console.WriteLine($"Hello, my name is {FullName}.");
}
}
// Derived class 'Student' inherits from 'Person'
public class Student : Person
{
public string StudentNumber { get; set; }
public string Major { get; set; }
}
// Derived class 'Teacher' also inherits from 'Person'
public class Teacher : Person
{
public string EmployeeNumber { get; set; }
public string Department { get; set; }
}
class Program
{
static void Main()
{
// Create a Student object
var student = new Student
{
FirstName = "Alice",
LastName = "Smith",
Major = "Computer Science"
};
// Create a Teacher object
var teacher = new Teacher
{
FirstName = "Bob",
LastName = "Johnson",
Department = "Physics"
};
// Both 'student' and 'teacher' have access to the properties and methods of the 'Person' class.
Console.WriteLine(student.FullName); // Inherited from Person
student.Greet(); // Inherited from Person
Console.WriteLine(teacher.FullName); // Inherited from Person
teacher.Greet(); // Inherited from Person
}
}
5. Use Cases
- Sharing common code: When you have multiple classes that share the same set of properties or methods.
- Creating specialized versions of a class: A derived class is often a more specialized version of the base class.
- Framework development: Base classes in frameworks often provide core functionality that developers can extend to fit their specific needs.
6. Mini Practice Task
- Create a base class called
Animalwith aNameproperty and a methodEat()that prints a generic message like"{Name} is eating." - Create two derived classes,
DogandCat, that inherit fromAnimal. - Add a
Bark()method to theDogclass and aMeow()method to theCatclass. - In your
Mainmethod, create instances ofDogandCat, set their names, and call both their specific methods (Bark/Meow) and the inherited method (Eat).