C# ClassesLesson 31Free preview
C# Polymorphism
This lesson is part of C# · All levels
Summary
Polymorphism makes a single name method can have different behavior in each derivative class, applied with virtual in parent and override in derivative. Thus, the code that works on the parent class automatically uses the most appropriate derivative-owned version. Polymorphism runs alongside inheritance and interface.
Key points
- Method bases are virtually marked when they are allowed to be replaced by derivatives.
- The derivative method uses override to replace the parent's own behavior.
- Without virtualization or override, the derivative method actually hides the parent method using new rather than polymorphism.
- Polymorphism allows handling many types through a single reference of the base type.
- It can still be called if the parent behavior is still required.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.