Welcome back! Now that you have a solid understanding of classes and objects in C#, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).
Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.
Let's start by defining a simple base class:
C#1public class Person 2{ 3 public string Name { get; set; } 4 public int Age { get; set; } 5 6 public Person(string name, int age) 7 { 8 this.Name = name; 9 this.Age = age; 10 } 11 12 public void Display() 13 { 14 Console.WriteLine($"Name: {Name}, Age: {Age}"); 15 } 16}
In this C# example, the Person
class includes:
Name
and Age
properties.Display
method to print out the name and age.This class serves as the base class from which other classes can inherit.
Now, we define a derived class that inherits from the base class:
C#1// Defines the Student class that inherits from the Person class 2public class Student : Person 3{ 4 // New property specific to Student 5 public string Major { get; set; } 6 7 // Constructor to initialize properties, including those from the base class 8 public Student(string name, int age, string major) : base(name, age) 9 { 10 this.Major = major; 11 } 12 13 // Method to display student's information 14 public void DisplayStudent() 15 { 16 // Call the Display method from the Person class to show name and age 17 base.Display(); 18 // Display the student's major 19 Console.WriteLine($"Major: {Major}"); 20 } 21}
In this example, the Student
class inherits from the Person
class:
Name
and Age
properties and the Display
method from the Person
class.Major
.DisplayStudent
, that calls the base class Display
method and prints the student's major.base
keyword to call the constructor and methods of the Person
class.Finally, let's test the inheritance by creating and using objects of the derived class:
C#1public class Program 2{ 3 static void Main() 4 { 5 Student student = new Student("Bob", 25, "Computer Science"); 6 student.DisplayStudent(); 7 8 // Output: 9 // Name: Bob, Age: 25 10 // Major: Computer Science 11 } 12}
In this code:
Student
object.student
object calls the DisplayStudent
method, which internally calls the Display
method inherited from the Person
class and then prints the major.This demonstrates how inheritance allows for code reuse and maintains fundamental behavior while introducing new features in derived classes.
Inheritance in C# allows you to build on existing classes and reuse code efficiently. By using inheritance, you can create a class hierarchy that mirrors real-world relationships and provides a clear and maintainable structure for your applications.
Understanding and leveraging inheritance will enable you to design flexible, scalable, and organized object-oriented applications. With inheritance, you can extend existing functionalities, reduce code duplication, and develop more robust and readable programs.
Excited to start practicing? Let's move on and put this theory into action!