The chapter in short
Object-oriented programming (OOP) organises programs around objects that combine data and behaviour. Python supports OOP with classes.
The chapter covers classes and objects, attributes and methods, constructors, encapsulation, inheritance and polymorphism.
ClassObjectConstructorEncapsulationInheritancePolymorphism
Key points
- A class is a blueprint; an object is an instance of a class.
- __init__ is the constructor that sets up a new object.
- self refers to the current object inside methods.
- Encapsulation hides internal data behind methods.
- Inheritance lets a child class reuse and extend a parent class.
- Polymorphism lets different classes respond to the same method call in their own way.
Definitions
- 1
Class
A blueprint for creating objects.
- 2
Object
An instance of a class with its own data.
- 3
Constructor
A special method that initialises a new object.
- 4
Encapsulation
Bundling data with methods and restricting direct access.
- 5
Inheritance
A class acquiring attributes and methods from another class.
- 6
Polymorphism
The ability of different objects to respond to the same method in different ways.
Formulas
Class
class Student: def __init__(self, name): self.name = name def greet(self): return "Hello " + self.name
Inheritance
class Prefect(Student): pass