Definition: Polymorphism allows objects of different classes to be treated as objects of a common base class.
Benefit: Enables writing flexible, reusable code by calling methods on base types that can be implemented differently in derived classes.
2. Virtual Methods and Overriding
Virtual Method: A method in a base class that can be redefined (overridden) in derived classes.
Declared with the virtual keyword in the base class and overridden in derived classes using the override keyword.
Example:
csharp
Copy code
class Animal
{
public virtual void Speak() { Console.WriteLine("Animal sound"); }
}
class Dog : Animal
{
public override void Speak() { Console.WriteLine("Woof"); }
}
Calling the Base Method: Within an overridden method, the base class’s method can be called using base.MethodName().
3. Abstract Classes
Abstract Class: A class that cannot be instantiated and may contain abstract methods (methods without implementation).
Abstract Method: Must be implemented by any non-abstract derived class.
Syntax:
csharp
Copy code
abstract class Animal
{
public abstract void Speak();
}
class Dog : Animal
{
public override void Speak() { Console.WriteLine("Woof"); }
}
Purpose: Provides a common template for derived classes, enforcing certain behaviors across different implementations.
4. Interfaces
Definition: Interfaces define a contract that implementing classes must follow, but they do not provide implementations.
Syntax:
csharp
Copy code
interface IAnimal
{
void Speak();
}
class Dog : IAnimal
{
public void Speak() { Console.WriteLine("Woof"); }
}
Key Points:
Interfaces contain only method signatures.
A class can implement multiple interfaces, unlike inheritance which is limited to one base class.
5. Static vs. Dynamic Types in Polymorphism
Static Type: The type of the variable as declared at compile time (e.g., Animal myAnimal).
Dynamic Type: The actual instance type that the variable refers to at runtime (e.g., Dog or Cat assigned to Animal myAnimal).
Example:
csharp
Copy code
Animal myAnimal = new Dog(); // Static type is Animal, dynamic type is Dog
myAnimal.Speak(); // Calls Dog's Speak method
6. Polymorphic Assignment
Polymorphic Behavior: Allows a variable of a base type to hold references to derived types, enabling the use of derived class methods overridden from the base.
Example:
csharp
Copy code
Animal a = new Dog();
Animal b = new Cat();
a.Speak(); // Calls Dog's Speak
b.Speak(); // Calls Cat's Speak
7. Liskov Substitution Principle (LSP)
Principle: Any instance of a derived class should be able to replace an instance of its base class without altering the correctness of the program.
Example: If Dog is derived from Animal, Dog should work wherever Animal is expected, adhering to the same interface or behavior contract as Animal.
Quick Exam Tips for MCQs
Differentiate Abstract Classes and Interfaces: Know that abstract classes can contain implementations, but interfaces cannot.
Recognize Polymorphic Behavior: Be clear on how static and dynamic types work with overridden methods.
Understand Key Terms: Virtual methods, abstract methods, and interface requirements are often tested.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (