C# — Polymorphism
Virtual methods
class Animal {
public virtual void Sound() {
Console.WriteLine("Some sound");
}
}
class Dog : Animal {
public override void Sound() {
Console.WriteLine("Bark");
}
}
Runtime polymorphism
Animal myAnimal = new Dog();
myAnimal.Sound(); // Bark
Mini Practice
- Override methods
- Use virtual/override
- Practice polymorphism
- Use base keyword
Up Next
Continue with Interfaces - Interface types.
Related Topics
Frequently Asked Questions about Polymorphism
What is Polymorphism in C#?
Polymorphism is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Polymorphism?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Polymorphism.
Why is Polymorphism important in C#?
Polymorphism is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.