Base Classes and Subclasses
Implementation of inheritance in Python is done through base classes (parents) and subclasses (children).
A base class defines general properties and behaviors, while a subclass inherits these features and can extend them with its own.
example.py
Employee is the base class. It stores the data that all employees share. Manager and Developer are subclasses of Employee. They automatically get everything from the base class, so they do not need to rewrite the same code.
The super() call runs the base class constructor, so name and salary are set correctly. Each subclass then adds its own data:
ManageraddsdepartmentDeveloperaddslanguage
Both subclasses also override get_info(). They reuse the base version with super().get_info() and then extend the result with their own details.
This makes the base class the foundation, and the subclass the specialized implementation that adds flexibility.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
Base Classes and Subclasses
Swipe to show menu
Implementation of inheritance in Python is done through base classes (parents) and subclasses (children).
A base class defines general properties and behaviors, while a subclass inherits these features and can extend them with its own.
example.py
Employee is the base class. It stores the data that all employees share. Manager and Developer are subclasses of Employee. They automatically get everything from the base class, so they do not need to rewrite the same code.
The super() call runs the base class constructor, so name and salary are set correctly. Each subclass then adds its own data:
ManageraddsdepartmentDeveloperaddslanguage
Both subclasses also override get_info(). They reuse the base version with super().get_info() and then extend the result with their own details.
This makes the base class the foundation, and the subclass the specialized implementation that adds flexibility.
Thanks for your feedback!