C# Polymorphism
Polymorphism is a concept in object-oriented programming that refers to the ability of a single interface to be used to refer to multiple implementations of a particular behavior. In C#, polymorphism can be achieved through inheritance, interfaces, and virtual methods.
Here is an example of polymorphism using inheritance in C#:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
public class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}
public class Triangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a triangle");
}
}
public static void Main()
{
Shape shape1 = new Shape();
Shape shape2 = new Circle();
Shape shape3 = new Rectangle();
Shape shape4 = new Triangle();
shape1.Draw(); // Outputs "Drawing a shape"
shape2.Draw(); // Outputs "Drawing a circle"
shape3.Draw(); // Outputs "Drawing a rectangle"
shape4.Draw(); // Outputs "Drawing a triangle"
}
In this example, the Shape
class has a Draw
method that is marked as virtual
. This means that derived classes can override this method to provide their own implementation. The Circle
, Rectangle
, and Triangle
classes all inherit from Shape
and override the Draw
method to provide their own implementation.
When we create instances of these classes and call the Draw
method on them, the correct implementation is called based on the type of the object. For example, when we call shape2.Draw()
, the implementation in the Circle
class is called because shape2
is an instance of Circle
.
In C#, there are a few rules to follow when using polymorphism:
- A class can only inherit from a single base class, but it can implement multiple interfaces.
- A method marked as
virtual
in the base class can be overridden in a derived class using theoverride
keyword. - If a derived class wants to call the implementation of a
virtual
method from the base class, it can use thebase
keyword. - If a derived class wants to prevent a
virtual
method from being overridden in further derived classes, it can use thesealed
keyword. - If a derived class wants to provide its own implementation of a
virtual
method, but also wants to call the implementation from the base class, it can use thebase
keyword in the implementation.
Here is an example that demonstrates these rules:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
public class Circle : Shape
{
public override void Draw()
{
// Call the implementation in the base class
base.Draw();
Console.WriteLine("Drawing a circle");
}
}
public class Rectangle : Shape
{
public sealed override void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}
public class Triangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a triangle");
}
}
public class Square : Rectangle
{
// This will cause a compile-error because the Draw method is sealed in the Rectangle class
public override void Draw()
{
Console.WriteLine("Drawing a square");
}
}
In this example, the Circle
class overrides the Draw
method and calls the implementation in the base class using the base
keyword. The Rectangle
class overrides the Draw
method and uses the sealed
keyword to prevent further derived classes from overriding it. The Square
class tries to override the Draw
method, but this causes a compile-error because the method is sealed in the Rectangle
class.
Polymorphism allows us to create a single interface for multiple implementations, which can make our code more flexible and maintainable.
I hope this helps! Let me know if you have any questions.