Inheritance & Polymorphism
What Is Inheritance?
Inheritance is like a family recipe book. Your grandma wrote a recipe for basic bread. Your mom inherited that recipe and added cinnamon to make cinnamon bread. You inherited mom's recipe and added frosting to make cinnamon rolls. Each generation builds on what came before without rewriting the whole recipe.
In C#, a child class (also called a "derived class") inherits all the properties and methods from its parent class (the "base class"). The child can then add new stuff or change existing behavior.
C# uses a colon : for inheritance: class Child : Parent
Basic Inheritance
Polymorphism — One Interface, Many Forms
Polymorphism is a fancy word for a simple idea (used everywhere in data structures like binary search trees): you can treat a child object as if it were the parent type, and the right method still gets called. It's like having a remote control that works on any TV brand — you press "volume up" and each TV handles it its own way.
This is incredibly powerful. You can write code that works with the parent type, and it automatically works with any child type — even ones that don't exist yet!
Polymorphism & Abstract Classes
Key Keywords Summary
virtual— "Children may override this method if they want to."override— "I'm replacing my parent's implementation with my own."abstract— "I'm not providing an implementation. Every child must provide one." Used on classes and methods.sealed— "No one can inherit from this class." It's the end of the family line.base— "Call my parent's constructor or method." Like calling grandma for the original recipe.