OOP helps in organizing code by grouping related data and functionality into classes and objects.
Purpose: Shows the basics of creating classes and objects.
- Class: A blueprint for creating objects (e.g.,
Car
). - Object: An instance of a class (e.g.,
my_car
). - Attributes: Characteristics of an object (e.g.,
make
,model
). - Methods: Functions defined inside a class (e.g.,
display_info
).
Purpose: Demonstrates how to use inheritance to extend classes.
- Inheritance: A way to create a new class from an existing class (e.g.,
ElectricCar
inherits fromVehicle
). - Subclass: The new class that inherits (e.g.,
ElectricCar
). - Superclass: The class being inherited from (e.g.,
Vehicle
).
Purpose: Covers polymorphism and encapsulation.
- Polymorphism: Allows different classes to be treated through a common interface (e.g.,
Shape
class witharea
method). - Encapsulation: Hides internal data and only exposes necessary parts (e.g., using private attributes in
Rectangle
).
Purpose: Shows how to use special (magic) methods in Python.
- Special Methods: Methods with double underscores (e.g.,
__add__
,__eq__
). - Usage: Define how objects behave with built-in operations (e.g., addition, comparison).