Chapter 11: Object-Oriented Programming (OOP)
Learn the basics of object-oriented programming in Python, including classes, objects, inheritance, and encapsulation.
In this chapter, we’ll cover the fundamentals of object-oriented programming (OOP) in Python. OOP allows you to structure your code around objects and classes, making it easier to model real-world scenarios. We’ll explore key concepts such as classes, objects, inheritance, and encapsulation.
What is Object-Oriented Programming?
Object-oriented programming (OOP) is a programming paradigm that structures code around objects and classes. This approach allows for modular, reusable, and organized code, especially useful in large applications.
Classes and Objects
A class is a blueprint for creating objects, while an object is an instance of a class. In Python, classes are defined using the class
keyword:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Alice", 30)
print(person1.name)
In this example, Person
is a class with attributes name
and age
. person1
is an object created from this class.
The __init__
Method
The __init__
method is a special method in Python classes. It’s automatically called when a new object is created and is used to initialize attributes:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.make, car1.model)
In this example, the __init__
method initializes the make
and model
attributes for each Car
object.
Methods in Classes
Methods are functions defined inside a class that operate on the class’s attributes. They allow objects to perform actions:
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} says Woof!")
In this example, the bark
method allows each Dog
object to perform an action.
Inheritance
Inheritance allows a class to inherit attributes and methods from another class. The new class is called the child class, and the class it inherits from is the parent class:
class Animal:
def eat(self):
print("Eating")
class Cat(Animal):
def meow(self):
print("Meow")
cat1 = Cat()
cat1.eat()
cat1.meow()
In this example, Cat
inherits from Animal
, allowing it to use the eat
method.
Encapsulation
Encapsulation is the concept of restricting direct access to certain attributes or methods. In Python, a leading underscore _
is used to indicate that an attribute or method is intended to be private:
class BankAccount:
def __init__(self, balance):
self._balance = balance
def deposit(self, amount):
self._balance += amount
def get_balance(self):
return self._balance
In this example, _balance
is intended to be a private attribute, and get_balance
provides controlled access to it.
Polymorphism
Polymorphism allows different classes to be used interchangeably, even if they have different implementations for the same method:
class Bird:
def sound(self):
print("Chirp")
class Dog:
def sound(self):
print("Bark")
animals = [Bird(), Dog()]
for animal in animals:
animal.sound()
In this example, both Bird
and Dog
classes have a sound
method, and we can call sound
on each object without knowing its specific class.
Summary and Next Steps
In this chapter, we covered the basics of object-oriented programming in Python, including classes, objects, inheritance, encapsulation, and polymorphism. Understanding these concepts is essential for writing structured, maintainable code. In the next chapter, we’ll learn how to interact with APIs and handle JSON data.