In C#, inheritance and polymorphism are two fundamental concepts of object-oriented programming. Inheritance allows you to create a new class that is a modified version of an existing class, while polymorphism allows you to use objects of different classes in a unified way.
Here are some of the key concepts related to inheritance and polymorphism in C#:
1. Inheritance: Inheritance allows you to create a new class that is a modified version of an existing class. The new class, called the derived class, inherits the properties and methods of the existing class, called the base class, and can add new properties and methods or modify existing ones.
Here is an example of a class hierarchy using inheritance:
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog barks");
}
}
This code defines an “Animal” class with a “MakeSound” method that prints a message to the console, and a “Dog” class that inherits from the “Animal” class and overrides the “MakeSound” method to print a different message.
2. Polymorphism: Polymorphism allows you to use objects of different classes in a unified way. This means that you can write code that works with objects of a base class and its derived classes without knowing the specific type of the objects at runtime.
Here is an example of polymorphism in C#:
Animal animal1 = new Animal(); Animal animal2 = new Dog(); animal1.MakeSound(); // prints "The animal makes a sound" animal2.MakeSound(); // prints "The dog barks"
This code creates an “Animal” object and a “Dog” object, and assigns them to variables of type “Animal”. The “MakeSound” method is called on both objects, but because the “MakeSound” method is overridden in the “Dog” class, the output is different for each object.
3. Abstract classes and interfaces: Abstract classes and interfaces are two other ways to define class hierarchies in C#. Abstract classes are classes that cannot be instantiated directly and are intended to be used as base classes for derived classes. Interfaces are similar to abstract classes, but they define a set of methods and properties that a class must implement.
Here is an example of an abstract class and an interface in C#:
public abstract class Shape
{
public abstract double Area { get; }
}
public interface IResizable
{
void Resize(double factor);
}
public class Rectangle : Shape, IResizable
{
public double Width { get; set; }
public double Height { get; set; }
public override double Area => Width * Height;
public void Resize(double factor)
{
Width *= factor;
Height *= factor;
}
}
This codedefines an abstract class named “Shape” with an abstract property “Area”, and an interface named “IResizable” with a “Resize” method. The “Rectangle” class inherits from “Shape” and implements “IResizable”, allowing it to calculate its area and resize itself.
Inheritance and polymorphism are powerful concepts in C# that allow you to create complex class hierarchies and write code that is flexible and reusable. By using inheritance and polymorphism, you can create code that is easier to understand, maintain, and extend.