There are many approaches you can take in Python to solve a programming problem, but there is one very popular approach to solve problems and it is called Object-Oriented Programming, and it is also knowns as OOPS which is of course its acronym.
An object can be a simple representation of a smartphone or a move made by the computer in a video game.
Object-oriented programming opens many ways to explore the beauty of programming which can be used to store information about real situations.
There are two important parts of an Object:
- Attributes: Let us take an example of a fan, that fan can be of the color black and it might have 4 blades or wings. These things would be the fan’s properties or attributes.
- Behavior: Now, the fan can be at rest or at motion, it might be dirty or clean, it is maybe damaged or in perfect condition, now these things are the fan’s behavior.
So when it comes to real life, anything can be described with some certain form of information (like the number of blades, speed, etc.), so it makes sense that programming languages should also take advantage of this.
Also, with that certain form of information, you can describe a different fan that might have a lesser number of fan blades or it might rotate at slower speeds.
So what exactly you can understand from the example given above:
- First, if we implement this in a programming language, we can generate many applications from the code.
- Second and equally important, the code can be over and over again among different users and it also shortens the main code to a certain level.
We are going to discuss some important parts of object-oriented programming and how they can be used at a basic level.
There can be multiple objects, a Class is a way to store the objects.
A class may have its own attributes and functions, let us take an example of a real-life example to understand a class. Attributes are the variables that are present inside a class.
Example: If you have a class of tech devices, then the attributes and functions of the class would be the name of the device, color, year of manufacturing, etc.
A class in Python can be created like this:
We created a class using the class keyword that tells Python that the user is defining a class. Then the class keyword is followed by the class name which in this case is Car().
It is recommended to keep the first letter of the class name capital and the first letter of methods/functions small, by doing this someone else who is reading your code will not get confused.
To use whatever is inside the class, we have to create an instance, it is basically just an object for a specific class.
Whenever you read the word “instance”, do not get confused because the words “instance” and “object” are often used interchangeably in Python.
So let us add some stuff in our class that we created above and use it with the help of objects!
An object is a part of a class that has some property and behavior. Everything in an object-oriented language is an object.
Now let us use a class and some objects to get a useful piece of code.
Have a look at what is happening in the function above:
- We define a function with the name of __init__(), this function is called a constructor and gets executed every time an object is created.
- The self keyword makes the attribute available to all of the methods of the class and we initialize the value of name to be accessed by the whole class.
- We define a function that is also available to the whole class. This function prints a message to the screen.
- Here, we are creating an object name as “device1” which contains some information about the device, to keep things simple, we have only used one form of information i.e., the name of the device to describe the object. This is called object instantiation.
We call the brag_function using the dot notation.
Methods are the functions that are associated with some kind of object or class, they may have some attributes present in them. A class cannot be of much use without its own methods.
In the class created above, we have created two methods named __init__() and the brag_function().
We have named one of our methods as “brag_function” and generally speaking it does not become a big deal to use the words “function” and “method” interchangeably.
However, using the word “method” for functions inside a class is a good practice and you must use it because it will make your code less confusing to read.
Important Concepts of OOPS
The ability to have multiple forms is called polymorphism, the word might sound complicated but the whole concept is really easy to understand.
In Python, it is generally associated with a function and described as the ability of a function to be used in different ways
Let us see a concrete example to understand the concept:
Above we have created a class with two methods to initializes attributes and print an output to the screen, now let us create objects to use those functions.
As you can see in the example above, both the functions __init__() and print_details() have been used twice and they gave use two different outputs each time. This is polymorphism.
Inheritance might be one of the most important reasons for the importance of object-oriented programming. The word inheritance in programming means to derive something similar.
Multiple classes can be inherited from a single class and those classes would be called children classes of the parent class or base class. This plays a major role in the reusability of the code and hence makes the object-oriented programming so useful.
It is a very vast topic, so we are not going to discuss it in this article.
Encapsulation is also a very essential part of object-oriented programming. Encapsulation makes the methods and variables inaccessible from the outside.
This provides an accidental modification of code from a different user. You can choose what part of the code other users can modify and what they cannot.