Understanding Inheritance in C#
Inheritance is one of the key features of object-oriented programming (OOP) in C#. It allows one class to inherit the properties and methods of another class. This promotes code reusability and establishes a natural hierarchy between classes. In this article, we will explore the intricacies of inheritance in C#, complete with examples and explanations tailored for beginners.
1. Introduction to Inheritance
Inheritance in C# enables the creation of a new class based on an existing class. The new class, known as the derived class, inherits all the data and behaviors (methods) from the base class. This supports code reuse and allows for the creation of a polymorphic architecture.
2. Base Class and Derived Class
Term | Description |
---|---|
Base Class | The class from which properties and methods are inherited. |
Derived Class | The class that inherits properties and methods from the base class. |
2.1 Base Class
The base class is the parent class from which data members and functions are inherited. For example, consider a class Animal.
public class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine(Name + " is eating.");
}
}
2.2 Derived Class
The derived class extends the base class and can add its own members or override existing members.
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine(Name + " says Woof!");
}
}
3. Creating a Base Class
When creating a base class, you define properties and methods that can be reused. Here’s a simple example:
public class Vehicle
{
public string Brand { get; set; }
public void Drive()
{
Console.WriteLine(Brand + " is driving.");
}
}
4. Creating a Derived Class
To create a derived class, you use the : operator followed by the base class name:
public class Car : Vehicle
{
public int NumDoors { get; set; }
public void Honk()
{
Console.WriteLine(Brand + " says Beep!");
}
}
5. Accessing Base Class Members
You can access the base class members directly using the derived class object.
Car myCar = new Car();
myCar.Brand = "Toyota";
myCar.Drive();
myCar.Honk();
6. Using the base Keyword
The base keyword allows you to access base class members from a derived class. Here’s an example of calling a base class constructor:
public class Car : Vehicle
{
public Car(string brand)
{
base.Brand = brand;
}
}
7. Method Overriding
Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class.
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
8. Polymorphism
Polymorphism allows methods to have the same name but behave differently based on the object that it is invoked on. It can be categorized into compile-time and run-time.
8.1 Compile-Time Polymorphism
Compile-time polymorphism is achieved through method overloading, i.e., multiple methods having the same name with different parameters.
public class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
8.2 Run-Time Polymorphism
Run-time polymorphism is achieved through method overriding, which we discussed in the previous sections. It allows methods to be invoked at runtime based on the object type.
Animal myAnimal = new Dog();
myAnimal.Speak(); // Output: Dog barks
9. The virtual and override Keywords
In order to override a method in a derived class, the method in the base class must be marked as virtual. Conversely, in the derived class, you use the override keyword.
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
public override void Speak()
{
Console.WriteLine("Dog barks");
}
10. Conclusion
Inheritance is a fundamental concept in C# that simplifies your code and helps in creating a natural hierarchy. By understanding how to create base and derived classes, access their members, use polymorphism, and implement method overriding, you can enhance your programming skills and create more adaptable software.
FAQ
- What is inheritance in C#? Inheritance allows a new class to inherit properties and methods from an existing class.
- What are the types of inheritance in C#? The primary types are single inheritance, multiple inheritance (via interfaces), and hierarchical inheritance.
- Can a derived class access private members of the base class? No, private members are not accessible. You can use protected modifiers for such cases.
- What is the difference between overriding and overloading? Overriding changes the behavior of a base class method in the derived class, while overloading allows the same method to have different parameter lists.
- What is polymorphism? Polymorphism allows methods to behave differently based on the object that invokes them.
Leave a comment