Inheritance Basics
beginnerInheritance allows a new class to inherit properties and behaviors from an existing class.
Base Class (Parent)
The class being inherited from.
Derived Class (Child)
The class that inherits.
Syntax
Dog inherits eat() from Animal and adds bark().
Types of Inheritance
- public: Most common. Public stays public, protected stays protected
- private: Everything becomes private in derived class
- protected: Public becomes protected in derived class
Benefits
- Code reuse: Write common code once in base class
- Logical hierarchy: Model real-world relationships
- Polymorphism: Treat derived classes through base class interface
Example Hierarchy
Animal -> Dog, Cat, Bird
Vehicle -> Car, Truck, Motorcycle
Key takeaway: Inheritance enables code reuse and logical hierarchies — prefer public inheritance and compose when possible.