OOP Concepts
**
| Object | - Entity that contains data and procedures |
| Encapsulation | - Combining data and procedure into a single object |
| Data Hiding | - Object’s data attributes are hidden from code outside the object and access is restricted to the object’s methods - Advantages - Protects from accidental corruption - Outside code does not need to know internal structure of the object |
| Class | - Code that specifies the data attributes and methods of a particular type of object |
| Instance | - An object created from a class |
| Inheritance | - Creates an ‘is a’ relationship between classes - The subclass Inherits attributes and methods of the superclass |
**
Class: code that specifies the data attributes and methods of a particular type of object
Object: entity that contains data and procedures
Encapsulation: combining data and procedure into a single object
Data hiding: object’s data attributes are hidden from code outside and access is restricted to the object’s methods
- protects from accidental corruption
- outside code does not need to know the internal strucutre of the object
Get - returns an attribute
set - changes an attribute
Instance: an object created from a class

Inheritance: allows a new class to acquire the attributes and methods of an existing class.

Polymorphism: an object can take different forms. It allows different classes to have methods with the same name but different implementations.
class 25S6D:
def __init__(self, size):
self.size = size
self.names = [‘refreshed’] * size
def output(self):
for i in range(self.size):
print(self.names[i])
# attributes: names and selfObject: entity that contains data and procedures
Advantages of OOP
- Provides a clear structure to programs
- Makes code easier to maintain, reuse, and debug
- Helps keep your code DRY (Don’t Repeat Yourself)
- Allows you to build reusable applications with less code
| Class | Objects |
|---|---|
| Fruit | Apple, Banana, Mango |
| Car | Volvo, Audi, Toyota |
![]() |
import random
# The Coin class simulates a coin that can be flipped.
class Coin:
# initializes the sideup data attribute with 'Heads'.
def __init__(self):
self.sideup = 'Heads'
# The toss method generates a random number in the range of 0
# to 1. If the number is 0, then sideup is set to 'Heads'.
# Otherwise, sideup is set to 'Tails'.
def toss(self):
if random.randint(0, 1) == 0:
self.sideup = 'Heads'
else:
self.sideup = 'Tails'
# The get_sideup method returns the value referenced by sideup
def get_sideup(self):
return self.sideupThe init() Method
All classes have a built-in method called __init__(), which is always executed when the class is being initiated.
The __init__() method is used to assign values to object properties, or to perform operations that are necessary when the object is being created.
Without the __init__() method, you would need to set properties manually for each object
Using __init__() makes it easier to create objects with initial values
__init__ runs automatically whenever you create a new object.
Self Parameter
The self parameter is a reference to the current instance of the class.
self means “this particular object”.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name)
p1 = Person("Emil", 25)
p1.greet()can access anything using self
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def display_info(self):
print(f"{self.year} {self.brand} {self.model}")
car1 = Car("Toyota", "Corolla", 2020)
car1.display_info()