Mihai Domocos
Brașov Get in touch
Chapter 8

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that uses objects to represent and manipulate data. In Python, everything is an object. Understanding OOP is important for writing complex programs and creating reusable code. This chapter will cover the following topics:

Classes and objects

A class is a blueprint for creating objects, and an object is an instance of a class. In Python, classes are defined using the class keyword. Objects can have attributes (data) and methods (functions).

Example:

python
1class Car:
2 def __init__(self, make, model, year):
3 self.make = make
4 self.model = model
5 self.year = year
6
7 def description(self):
8 return f"{self.year} {self.make} {self.model}"
9
10my_car = Car("Toyota", "Corolla", 2020)
11print(my_car.description()) # Output: "2020 Toyota Corolla"

Methods and attributes

Methods are functions that are associated with a class or an object, and attributes are data stored in an object or a class. In Python, methods are defined like functions, but they are associated with a class or an object using the dot notation.

Example:

python
1class Rectangle:
2 def __init__(self, length, width):
3 self.length = length
4 self.width = width
5
6 def area(self):
7 return self.length * self.width
8
9 def perimeter(self):
10 return 2 * (self.length + self.width)
11
12my_rect = Rectangle(5, 3)
13print(my_rect.area()) # Output: 15
14print(my_rect.perimeter()) # Output: 16

Inheritance and polymorphism

Inheritance is a mechanism for creating a new class that is a modified version of an existing class. The new class inherits the properties and methods of the existing class. Polymorphism is the ability of an object to take on many forms. In Python, polymorphism is achieved through inheritance and method overriding.

Example:

python
1class Animal:
2 def __init__(self, name):
3 self.name = name
4
5 def sound(self):
6 pass
7
8class Cat(Animal):
9 def sound(self):
10 return "Meow"
11
12class Dog(Animal):
13 def sound(self):
14 return "Woof"
15
16my_cat = Cat("Tom")
17my_dog = Dog("Spike")
18print(my_cat.sound()) # Output: "Meow"
19print(my_dog.sound()) # Output: "Woof"

Encapsulation and data hiding

Encapsulation is the concept of binding data and methods together within a class, and data hiding is the concept of restricting access to certain data and methods. In Python, data hiding is achieved through name mangling.

Example:

python
1class BankAccount:
2 def __init__(self, balance):
3 self.__balance = balance
4
5 def deposit(self, amount):
6 self.__balance += amount
7
8 def withdraw(self, amount):
9 if amount <= self.__balance:
10 self.__balance -= amount
11 else:
12 print("Insufficient balance")
13
14 def get_balance(self):
15 return self.__balance
16
17my_account = BankAccount(1000)
18my_account.deposit(500)
19print(my_account.get_balance()) # Output: 1500
20my_account.withdraw(2000) # Output: "Insufficient balance"

This site remembers your theme choice and, with your consent, uses cookies for analytics. Cookie policy