What is Object Oriented Programming?
Object-oriented programming (OOP) is a fundamental concept in many programming languages like Java, Python, and C++. It provides a powerful way to organize your code by mimicking real-world objects and their interactions. This blog post will serve as a gentle introduction to OOP concepts, followed by a step-by-step explanation of a sample code snippet that demonstrates these concepts in action.
Why Use OOP?
Traditional programming often relies on procedures and functions to manipulate data. While this works, it can become cumbersome and difficult to maintain as your program grows in complexity. OOP offers several advantages:
- Modularization: OOP breaks down your program into smaller, reusable blocks called objects. Each object encapsulates data (attributes) and the functions (methods) that operate on that data. This promotes better code organization and maintainability.
- Reusability: Objects can be reused throughout your program, reducing code duplication and promoting efficiency.
- Maintainability: Changes made to an object's internal workings are isolated, minimizing the impact on other parts of your program.
An Example
Let’s consider a scenario where we need to determine the area and perimeter of a rectangle using OOP. We create two functions within a class - one for calculating the area and another for calculating the perimeter. By utilizing this class, we can easily obtain the desired results. This example will demonstrate the implementation of OOP in Python.
1. Defining the Class:
To begin, we must first define a class using the class keyword. We will choose the name Rectangle for this class. The next step is to initialize and declare the variables associated with the class. To do this, we use the keyword def __init__() and list the variables within parentheses. Additionally, we use the term self, which serves as a handle to the variables, allowing us to easily access and manipulate them within functions. Within the initiation block, we then assign each variable to self by using self.length = length and self.width = width. This portion of the code looks like this:
class Rectangle:
def __init__(self,width,length):
self.length = length
self.width = width
2. Defining the Functions within the Class:
In the following, it is necessary to define the functions to calculate the two outputs in question: area and perimeter. To do this, first, the calculate_area function is written under the Rectangle class. The input of this function is the handle of variables, self. By using self, it is easy to input the length and width as self.length and self.width, respectively. The same approach is taken for the calculate_perimeter function, where the perimeter is calculated by using the handle self and importing the length and width. The necessary codes for this task are as follows:
def calculate_area(self):
return self.length * self.width
def calculate_perimeter(self):
return 2 * (self.length + self.width)
3. Creating an object of the class
The next step involves creating an instance of this class. This object inherits all the characteristics of the class but with initialized inputs. For instance, by setting the length to 10 meters and the width to 5 meters, we can create the desired object as shown below.
rectangle = Rectangle(5,10)
4. Calculating the desired parameters
It is now time to compute the area and perimeter. To do so, we can simply call the created object and utilize the area and perimeter functions within it. This can be achieved with just two lines of code, as illustrated below.
area = rectangle.calculate_area()
perimeter = rectangle.calculate_perimeter()
5. Printing the results
Once the desired parameters have been calculated, we can proceed to display their values. This can be accomplished by using the print command, as shown below.
print('The area of the rect: ', area)
print('The perimeter of the rect: ', perimeter)
Last word!
Following the principles of OOP has allowed us to easily calculate the area and perimeter as intended. Embracing OOP practices in programming will enhance the organization of your code, improve communication between different components, facilitate function and command calls, and simplify the implementation of complex logic.
I hope that the simple example provided in this post has helped you to better understand and apply OOP in your programming endeavors. You can explore the content of this post, along with additional explanations, in a video featuring Dr. Mahdi Shadabfar accessible through the links below. Your feedback on the post content is valuable to us, so please share your thoughts in the comments section.