C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we will explore how to implement polymorphism using interfaces in C#. Polymorphism is a fundamental concept in object-oriented programming that allows objects of different types to be treated as objects of a common type.
Let's start by defining an interface IShape
with a single method, Area
:
public interface IShape { double Area(); }
Now, we will create different classes (e.g., Circle
and Rectangle
) that implement the IShape
interface:
public class Circle : IShape { public double Radius { get; set; } public double Area() { return Math.PI * Math.Pow(Radius, 2); } } public class Rectangle : IShape { public double Width { get; set; } public double Height { get; set; } public double Area() { return Width * Height; } }
In this example, both the Circle
and Rectangle
classes implement the IShape
interface and provide their own implementation of the Area
method.
With the interface and implementing classes in place, we can now use polymorphism to treat objects of different types as objects of a common type (IShape
). This allows us to write more generic code that can work with any class implementing the IShape
interface:
public static void Main(string[] args) { IShape circle = new Circle { Radius = 5 }; IShape rectangle = new Rectangle { Width = 4, Height = 6 }; DisplayArea(circle); DisplayArea(rectangle); } public static void DisplayArea(IShape shape) { Console.WriteLine($"Area: {shape.Area():F2}"); }
In this example, the DisplayArea
method accepts a parameter of the IShape
interface type. This allows it to work with any object that implements the IShape
interface, such as Circle
and Rectangle
objects.
When we call DisplayArea
with different objects, the appropriate implementation of the Area
method is called based on the runtime type of the object. This is an example of polymorphism in action.
This tutorial demonstrates how to implement polymorphism using interfaces in C#. By defining a common interface and implementing it in different classes, you can treat objects of different types as objects of a common type, allowing you to write more generic and reusable code.
How to achieve polymorphism with interfaces in C#
Polymorphism in C# is achieved through interfaces by allowing objects of different classes to be treated uniformly if they implement the same interface. This allows for flexibility and extensibility in your code.
C# interface polymorphism example
Here's a basic example demonstrating polymorphism using interfaces:
using System; 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"); } } class Program { static void Main() { IShape shape1 = new Circle(); IShape shape2 = new Square(); shape1.Draw(); // Outputs: Drawing a circle shape2.Draw(); // Outputs: Drawing a square Console.ReadLine(); } }
Using interfaces for polymorphism in C#
Interfaces define a contract that multiple classes can implement, enabling polymorphic behavior. In the example above, Circle
and Square
can be treated as IShape
.
Dynamic polymorphism in C# interfaces
Dynamic polymorphism refers to the ability to determine the actual object type at runtime. Interfaces provide dynamic polymorphism in C#:
IShape shape = GetShape(); // Returns either Circle or Square dynamically shape.Draw(); // Calls the Draw method of the actual object type
C# polymorphism vs. inheritance
Polymorphism and inheritance often go hand-in-hand. Inheritance allows classes to share a common base, while polymorphism allows objects to be treated uniformly through interfaces or base classes.
Polymorphic behavior with interfaces in C#
Polymorphic behavior allows a single interface to be implemented by multiple classes, each providing its own implementation of the interface methods.
C# polymorphism and method overriding in interfaces
In interfaces, method overriding is implicit. When a class implements an interface, it must provide an implementation for all the interface methods.
C# polymorphism and interface inheritance
Interface inheritance allows one interface to inherit from another. This can be useful for creating a hierarchy of interfaces with increasingly specialized behavior.
Implementing polymorphic behavior in C# classes through interfaces
Classes implementing an interface can exhibit polymorphic behavior. In the example above, both Circle
and Square
exhibit polymorphic behavior as IShape
.
C# interface polymorphism and type casting
Type casting can be used to access specific members of a class beyond the interface. Example:
if (shape1 is Circle) { Circle circle = (Circle)shape1; // Access Circle-specific members }
C# polymorphism and abstract classes vs. interfaces
Abstract classes and interfaces both support polymorphism, but abstract classes can provide partial implementations, while interfaces cannot.
Real-world examples of polymorphism with interfaces in C#
Real-world examples include GUI frameworks where various controls (buttons, textboxes, etc.) can all implement a common IControl
interface, allowing them to be treated uniformly.