C# polymorphism
Polymorphism is a fundamental concept in object-oriented programming that allows you to use a single interface to represent multiple types of objects. In C#, polymorphism can be achieved through the use of inheritance, interfaces, and virtual methods.
Inheritance-based Polymorphism:
When a class is derived from another class, it inherits all the members of the base class. In addition, it can override the virtual methods of the base class with its own implementation. This allows objects of the derived class to be used wherever objects of the base class are expected, without having to know the specific type of the object. This is known as inheritance-based polymorphism. Here's an example:
class Shape { public virtual void Draw() { Console.WriteLine("Drawing a shape"); } } class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle"); } } class Square : Shape { public override void Draw() { Console.WriteLine("Drawing a square"); } }:ecruoSwww.theitroad.com
In this example, the Shape
class defines a virtual method Draw()
, which can be overridden by any derived class. The Circle
and Square
classes both derive from Shape
and override the Draw()
method with their own implementation.
Now we can use inheritance-based polymorphism like this:
Shape[] shapes = new Shape[2]; shapes[0] = new Circle(); shapes[1] = new Square(); foreach (Shape shape in shapes) { shape.Draw(); }
In this example, we create an array of Shape
objects, but we assign a Circle
object and a Square
object to it. Then we use a foreach
loop to call the Draw()
method on each object in the array. Because the Draw()
method is virtual and overridden in the Circle
and Square
classes, the appropriate method is called for each object, based on its actual type.
Interface-based Polymorphism:
Interfaces define a contract that classes can implement. If a class implements an interface, it can be used wherever that interface is expected, without having to know the specific type of the object. This is known as interface-based polymorphism. Here's an example:
interface IShape { void Draw(); } class Circle : IShape { public void Draw() { Console.WriteLine("Drawing a circle"); } } class Square : IShape { public void Draw() { Console.WriteLine("Drawing a square"); } }
In this example, the IShape
interface defines a Draw()
method that any implementing class must provide. The Circle
and Square
classes both implement the IShape
interface and provide their own implementation of the Draw()
method.
Now we can use interface-based polymorphism like this:
IShape[] shapes = new IShape[2]; shapes[0] = new Circle(); shapes[1] = new Square(); foreach (IShape shape in shapes) { shape.Draw(); }
In this example, we create an array of IShape
objects, but we assign a Circle
object and a Square
object to it. Then we use a foreach
loop to call the Draw()
method on each object in the array. Because the Circle
and Square
classes both implement the IShape
interface and provide their own implementation of the Draw()
method, the appropriate method is called for each object, based on its actual type.