Skip to main content
Practice

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm that represents real-world objects and abstract concepts as objects in a program.

In Object-Oriented Programming, a program is composed of independent objects with attributes and behaviors, and the interactions between these objects are used to implement the program.

Python supports Object-Oriented Programming by using the concepts of classes and objects to structure programs.

In this lesson, we will learn the basic concepts of Object-Oriented Programming and explore how to use them in Python.


What are Classes and Objects?

Representing the Real World in a Program

The things we use daily, like cars, books, and computers, have different characteristics and functions.

In programming, similar concepts are used to represent these things through classes and objects.


Class

A class acts like a blueprint for items like cars and books.

This blueprint defines the attributes and methods of an object.


Object

An object is an instance created from a class blueprint.

For example, an object created from a car class would represent an actual car, possessing attributes like brand, model, and year.


The Blueprint, Class

A class defines new data types in programming and specifies the attributes and behaviors of objects created from it.

Below is an example code that defines a simple class representing a car.

Defining a Car Class
class Car:
def __init__(self, brand, model, year):
# Define car's brand, model, and year attributes
self.brand = brand
self.model = model
self.year = year

# Method to start the car's engine
def start_engine(self):
print(f"Starting the engine of {self.brand} {self.model}")

# Method to stop the car's engine
def stop_engine(self):
print(f"Stopping the engine of {self.brand} {self.model}")

In the code above, the Car class defines attributes like the car's brand, model, and year and provides methods to start and stop the engine.

The __init__ method is called automatically when an object is created, initializing the object's attributes.

self is the first parameter in class methods, referring to the instance the method is called on.


Object: A Real-World Instance from a Blueprint

Now, let's create an object based on the class blueprint we defined.

Creating an object is akin to constructing an actual 'car' from a car blueprint.

In a program, objects are created from classes, and you can use these objects to utilize the attributes and behaviors defined in the class.

Creating an Object
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year

def start_engine(self):
print(f"Starting the engine of {self.brand} {self.model}")

def stop_engine(self):
print(f"Stopping the engine of {self.brand} {self.model}")

# Create an object my_car from the Car class blueprint
# with brand 'BrandX', model 'ModelY', and year 2024
my_car = Car("BrandX", "ModelY", 2024)

# Output: Starting the engine of BrandX ModelY
my_car.start_engine()

# Output: Stopping the engine of BrandX ModelY
my_car.stop_engine()

# Print the year of the car
print(my_car.year) # Output: 2024

Here, my_car is an object created from the Car class, representing a car with brand 'BrandX', model 'ModelY', and year 2024.

This object can perform actions like starting and stopping the engine through the defined methods.

Furthermore, the object's attribute year can be accessed by my_car.year to verify its value.


Benefits of Using Object-Oriented Programming

Using Object-Oriented Programming improves reusability and simplifies logical modularization of the code.

For example, defining a car class allows creating multiple objects representing different cars.

Each object can have attributes like brand, model, and year, with each object operating independently.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.