-
Notifications
You must be signed in to change notification settings - Fork 5
Polymorphism
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
Polymorphism in C# refers to the ability of objects to take on different forms. It allows methods to behave differently based on the object that calls them, enabling flexibility and reusability in code.
-
Compile-Time Polymorphism (Static Polymorphism)
- Achieved through method overloading and operator overloading.
- The behavior is determined at compile time.
-
Run-Time Polymorphism (Dynamic Polymorphism)
- Achieved through method overriding using inheritance.
- The behavior is determined at runtime.
Definition:
- Method overloading allows multiple methods with the same name but different parameters in the same class.
Example:
class Calculator {
public int Add(int a, int b) {
return a + b;
}
public double Add(double a, double b) {
return a + b;
}
public int Add(int a, int b, int c) {
return a + b + c;
}
}
// Usage
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(5, 3)); // Output: 8
Console.WriteLine(calc.Add(5.5, 3.3)); // Output: 8.8
Console.WriteLine(calc.Add(1, 2, 3)); // Output: 6
Definition:
- A derived class overrides a base class method using the
virtual
andoverride
keywords.
Example:
class Animal {
public virtual void Speak() {
Console.WriteLine("The animal makes a sound.");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("The dog barks.");
}
}
class Cat : Animal {
public override void Speak() {
Console.WriteLine("The cat meows.");
}
}
// Usage
Animal animal1 = new Dog();
animal1.Speak(); // Output: The dog barks.
Animal animal2 = new Cat();
animal2.Speak(); // Output: The cat meows.
- Interfaces provide polymorphism by ensuring a class implements certain methods.
Example:
interface IShape {
void Draw();
}
class Circle : IShape {
public void Draw() {
Console.WriteLine("Drawing a circle.");
}
}
class Rectangle : IShape {
public void Draw() {
Console.WriteLine("Drawing a rectangle.");
}
}
// Usage
IShape shape1 = new Circle();
shape1.Draw(); // Output: Drawing a circle.
IShape shape2 = new Rectangle();
shape2.Draw(); // Output: Drawing a rectangle.
Term | Description |
---|---|
Method Overloading | Multiple methods with the same name but different signatures. |
Method Overriding | Redefining a method in a derived class to change its behavior. |
Virtual | Indicates that a method can be overridden in a derived class. |
Override | Used in the derived class to override a virtual method. |
Abstract | Defines a method without implementation in a base class, requiring derived classes to implement it. |
Interface | Provides a contract for classes to implement methods, enabling polymorphism. |
Aspect | Overloading | Overriding |
---|---|---|
Definition | Same method name, different parameters. | Redefining a method in the derived class. |
Polymorphism Type | Compile-time | Run-time |
Keyword | No special keywords. |
virtual , override , abstract
|
Class Relationship | Same class. | Requires inheritance. |
Example:
class BaseClass {
public virtual void Display(string message) {
Console.WriteLine("Base class message: " + message);
}
}
class DerivedClass : BaseClass {
public override void Display(string message) {
Console.WriteLine("Derived class message: " + message);
}
// Overloading
public void Display(string message, int count) {
for (int i = 0; i < count; i++) {
Console.WriteLine(message);
}
}
}
// Usage
BaseClass baseObj = new BaseClass();
baseObj.Display("Hello from Base!"); // Output: Base class message: Hello from Base!
DerivedClass derivedObj = new DerivedClass();
derivedObj.Display("Hello from Derived!"); // Output: Derived class message: Hello from Derived!
derivedObj.Display("Hello again!", 3); // Output: Hello again! (3 times)
- Polymorphism provides flexibility by allowing a single interface to represent different types of behavior.
-
Types:
- Compile-Time: Method overloading and operator overloading.
- Run-Time: Method overriding using inheritance.
- Use polymorphism to enhance code reusability, maintainability, and extensibility.
Polymorphism is a cornerstone of object-oriented programming, ensuring better design and reduced redundancy.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.