Polymorphism
Polymorphism is a core concept in object-oriented programming that allows you to use a single interface to represent different types of objects. It enables you to write code that can work with objects of different classes, as long as they implement the same interface or inherit from the same base class.
In C#, polymorphism is achieved through method overriding. Method overriding allows a subclass to provide a different implementation of a method that is already defined in its base class. This allows you to call the same method on different objects and get different behaviors depending on the actual type of the object.
Here's an example that demonstrates polymorphism in C#:
1using System;
2
3public class Shape
4{
5 public virtual void Draw()
6 {
7 Console.WriteLine("Drawing a shape...");
8 }
9}
10
11public class Circle : Shape
12{
13 public override void Draw()
14 {
15 Console.WriteLine("Drawing a circle...");
16 }
17}
18
19public class Rectangle : Shape
20{
21 public override void Draw()
22 {
23 Console.WriteLine("Drawing a rectangle...");
24 }
25}
26
27public class Program
28{
29 static void Main()
30 {
31 Shape shape = new Circle();
32 shape.Draw();
33
34 shape = new Rectangle();
35 shape.Draw();
36 }
37}
In this example, we have a base class Shape
with a virtual method Draw()
. We then define two derived classes, Circle
and Rectangle
, which override the Draw()
method.
In the Main()
method, we create a Shape
variable shape
and assign it an instance of Circle
. When we call the Draw()
method on shape
, the overridden Draw()
method in the Circle
class is invoked, and the output is "Drawing a circle...". Similarly, when we assign an instance of Rectangle
to the shape
variable and call the Draw()
method, the overridden Draw()
method in the Rectangle
class is invoked, and the output is "Drawing a rectangle...".
Polymorphism allows for code reuse, flexibility, and modularity. By writing code that depends on abstractions rather than concrete implementations, you can easily extend and modify your code without affecting other parts of the program. It promotes loose coupling and is a fundamental principle in object-oriented design.
Polymorphism has many real-world applications. For example, in a drawing application, you can have a Canvas
that contains a collection of Shape
objects. Regardless of the specific types of the shapes, you can call the Draw()
method on each shape and have it draw itself without knowing the exact class of the shape.
In summary, polymorphism is a powerful concept in object-oriented programming that allows you to write flexible and reusable code. It enables you to work with objects of different types through a common interface or base class, providing extensibility and modularity to your programs. Embracing polymorphism can lead to more maintainable and scalable codebases, which is especially important in larger software projects.
xxxxxxxxxx
}
using System;
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 Program
{
static void Main()