By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to be treated as instances of their parent class rather than their actual class. This enables a single interface to represent different underlying forms (data types). In C#, polymorphism is achieved using the virtual, override, and new keywords. Understanding these keywords is crucial for writing flexible, maintainable, and reusable code. Misunderstanding polymorphism can lead to code that is difficult to extend and maintain, resulting in bugs and inefficiencies. For instance, incorrectly using the new keyword instead of override can lead to unexpected behavior and make your code harder to debug.
csharp public class Animal { public virtual void Speak() { Console.WriteLine("The animal makes a sound."); } }
⚠️ Pitfall: Not marking methods as virtual if you intend to override them.
Override Methods in Derived Classes
csharp public class Dog : Animal { public override void Speak() { Console.WriteLine("The dog barks."); } }
⚠️ Pitfall: Forgetting to use the override keyword.
Use the New Keyword for Method Hiding
csharp public class Cat : Animal { public new void Speak() { Console.WriteLine("The cat meows."); } }
⚠️ Pitfall: Confusing method hiding with overriding.
Demonstrate Polymorphism
csharp Animal myDog = new Dog(); Animal myCat = new Cat(); myDog.Speak(); // Outputs: The dog barks. myCat.Speak(); // Outputs: The animal makes a sound.
Experts view polymorphism as a tool for designing flexible and extensible systems. They focus on the intent behind method overriding and hiding, understanding that overriding is for extending behavior polymorphically, while hiding is for redefining behavior in a non-polymorphic way. This distinction helps them write code that is both predictable and adaptable.
Exam trap: Questions that require distinguishing between overriding and hiding.
The mistake: Not marking base class methods as virtual.
Exam trap: Identifying why a method cannot be overridden.
The mistake: Expecting method hiding to work polymorphically.
Exam trap: Scenarios where method hiding is used incorrectly.
The mistake: Forgetting to use the override keyword.
Scenario: You are designing a system for a zoo where different animals make different sounds.Question: How would you implement the Speak method for a Bird class that inherits from Animal? Solution: 1. Define the Bird class inheriting from Animal.2. Override the Speak method in the Bird class.3. Implement the Speak method to output "The bird chirps." Answer:
public class Bird : Animal { public override void Speak() { Console.WriteLine("The bird chirps."); } }
Why it works: The override keyword allows the Bird class to provide a specific implementation of the Speak method, maintaining polymorphic behavior.
public override void MethodName()
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.