In object-oriented programming (OOP), inheritance is a fundamental concept that allows one class to inherit the properties and methods of another class. This promotes code reusability, reduces redundancy, and helps in creating a hierarchical classification of classes.
In this tutorial, we'll explore how to use inheritance in C# by extending existing classes and inheriting their properties and behaviors. We'll cover both basic and advanced aspects of inheritance, making it suitable for beginners as well as intermediate developers.
Inheritance is a mechanism where a new class (derived or child class) inherits the attributes and behaviors (methods) from an existing class (base or parent class). This allows the derived class to reuse the code of the base class, reducing duplication and promoting a clean and organized code structure.
C# supports single inheritance, meaning a class can inherit from only one base class. However, it supports multiple interfaces, allowing a class to implement multiple behaviors.
Let's dive into some practical examples to understand how inheritance works in C#.
Suppose we have a Vehicle class that represents a generic vehicle with properties like Make, Model, and Year. We can create a derived class called Car that inherits from Vehicle.
// Base class
public class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public void DisplayInfo()
{
Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
}
}
// Derived class
public class Car : Vehicle
{
public string Color { get; set; }
public void StartEngine()
{
Console.WriteLine("Engine started.");
}
}
In this example, the `Car` class inherits from the `Vehicle` class. It automatically gains all the properties and methods of `Vehicle`, including `Make`, `Model`, `Year`, and `DisplayInfo()`. Additionally, it has its own property `Color` and method `StartEngine()`.
### Using Inherited Properties and Methods
Here's how you can use the `Car` class:
```csharp
public class Program
{
public static void Main()
{
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2023;
myCar.Color = "Red";
myCar.DisplayInfo(); // Inherited method from Vehicle
myCar.StartEngine(); // Method specific to Car
}
}
<OutputBlock>
{`Make: Toyota, Model: Corolla, Year: 2023
Engine started.`}
</OutputBlock>
### Overriding Methods
You can override methods in the derived class to provide a specific implementation. For example, let's override the `DisplayInfo()` method in the `Car` class:
```csharp
public class Car : Vehicle
{
public string Color { get; set; }
public void StartEngine()
{
Console.WriteLine("Engine started.");
}
// Overriding the DisplayInfo method
public override void DisplayInfo()
{
base.DisplayInfo();
Console.WriteLine($"Color: {Color}");
}
}
Now, when you call `DisplayInfo()` on a `Car` object, it will display both the vehicle information and the color:
```csharp
public class Program
{
public static void Main()
{
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2023;
myCar.Color = "Red";
myCar.DisplayInfo(); // Overridden method in Car
myCar.StartEngine(); // Method specific to Car
}
}
Make: Toyota, Model: Corolla, Year: 2023 Color: Red Engine started.
You can also use protected access modifiers in the base class to allow derived classes to access certain members without exposing them to the outside world.
public class Vehicle
{
public string Make { get; set; }
protected string Model { get; set; } // Protected member
public int Year { get; set; }
public void DisplayInfo()
{
Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
}
}
public class Car : Vehicle
{
public string Color { get; set; }
public void StartEngine()
{
Console.WriteLine("Engine started.");
}
// Accessing protected member from derived class
public void ShowModel()
{
Console.WriteLine($"Model: {Model}");
}
}
In this example, the Model property is marked as protected, allowing it to be accessed within the Car class.
C# also supports abstract classes and methods, which must be implemented by any non-abstract derived class.
public abstract class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Abstract method
public abstract void StartEngine();
public void DisplayInfo()
{
Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
}
}
public class Car : Vehicle
{
public string Color { get; set; }
// Implementing the abstract method
public override void StartEngine()
{
Console.WriteLine("Engine started.");
}
}
In this example, Vehicle is an abstract class with an abstract method StartEngine(). The Car class must implement this method.
Now that you understand inheritance in C#, the next step is to explore Polymorphism in C#. Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling more flexible and dynamic code. Stay tuned for our next tutorial on polymorphism!
If you have any questions or need further clarification, feel free to ask in the comments below!