Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Base Classes and Subclasses | Section
Object-Oriented Programming in Python

bookBase 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

example.py

copy

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:

  • Manager adds department
  • Developer adds language

Both subclasses also override get_info(). They reuse the base version with super().get_info() and then extend the result with their own details.

Note
Note

This makes the base class the foundation, and the subclass the specialized implementation that adds flexibility.

question mark

What is the role of a subclass in Python inheritance?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 9

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookBase 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

example.py

copy

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:

  • Manager adds department
  • Developer adds language

Both subclasses also override get_info(). They reuse the base version with super().get_info() and then extend the result with their own details.

Note
Note

This makes the base class the foundation, and the subclass the specialized implementation that adds flexibility.

question mark

What is the role of a subclass in Python inheritance?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 9
some-alt