Hello programmers, so today we are going to learn about Object-oriented programming(OOP in short).
And in today's lesson we are going to learn about:
1. What is OOP?
2. Why do we need OOP?
3. How to efficiently use OOP to do awesome stuff in python.
What is OOP?
OOP is a programming method where coders/developers write code by creating classes and objects. Now, you might be thinking about what are classes and objects. Consider an example that you came for an industrial visit to the Tesla car factory. And you are inside the production line section of the factory and you noticed certain things about how a Tesla Model 3 car is being made. And those things are:
1. All Tesla Model 3 car was being made by robots.
2. Different robots are doing a certain specified task given to them at different sections in the production line where the raw materials were provided to them.
3. Tesla Inc. engineers were using python to instruct their robots to do their tasks.
Now let us recap on the things required to make a Tesla Model 3 car. We would be very discrete on this as too many components would be complicated to understand. So, What do we need to make a car?
1. Engine(Electric)
2. Body
3. Wheels
So, while making a Tesla Model 3 car, you noticed that the robots at Tesla Inc. were first making the engine, secondly, they were fitting the body, and lastly, they added the wheels.
Now, let us see how python helped Tesla Inc. engineers to make Tesla Model 3 cars. So, in python codes can be written in Classes and Objects. A class is a prototype of the car and programmers define or structure the prototype on how the car is going to be made. A class in python is defined by the keyword Class followed by the name of the class Eg:
Here a class is defined as Tesla model 3 and we are going to add some methods of instruction to the class so that the robots at Tesla could understand it.
As we can see just below the class there is a keyword "pass" now pass means that you have declared a class with the name Tesla_model_3 and you are going to add some method to it. When you are done doing that I(@python) will read the next line of code. Now let us look at what are methods.
What are the methods?
Methods are different types of functions that are used to perform the specific task inside a class. For eg. we are going to make an engine and add it to our car and similarly the other parts. The method is written as:
Here "def" is the declaration that a method is being used followed by the method name and the arguments of the method. We are going to define some methods to make our Tesla car.
Here we defined a method named __init__, this is a python reserve method to initialize the attribute to the class. It can also be called as a constructor because it can construct objects that are the instance of the class. This means that it is basically a prototype designed to create objects and in our case, it would be Tesla Model 3 cars.
Now let us create the prototype of our Tesla_model_3 cars. And in python you have to write the following codes:
We have finally written the prototype to create a tesla car. And here we have given four arguments inside the __init__ class and these are self, engine, body, and wheels. The self is a reference to the current instance of the class. It is used to access the variables inside the class and our engine, body, wheels are our variables."Self " keyword helps me(@python) reference the variable used by you. But till now we have just created the prototype of our car now let us actually create some cars.!!!!๐๐
To create a Tesla Model 3 car we just need to write our class name along with the arguments needed. Something like this:
So, we have created a Tesla model 3 car giving it a name car1 and it has an Electric motor as its engine with an aluminum body and 18" wheels. It is important to note that once you have created an object or car in our case then each object has its unique properties as given to them. For Eg. the robots at Tesla would create a certain number of cars every day and mark them as car1, car2, car3, etc. Here each car is unique and car1!=car2(!= means not equal). Also, while storing the objects in the computer the computer would store each object in different memory locations and as we can see above a funny-looking word "0x7f9c70aa5240" this is the memory address where our object is stored. And a memory address is just the address inside the computer where our object is stored on the computer.
And once the object is created it is the instance of the class. So, our final product car1, car2, and car3
are shown below:
Also, to find out the instance attribute of an object we can use a dot notation something like this:
Here we have two car car1 and car2 and we used dot notation to find our car1 engine type and car2 body type. Similarly, we can find any attribute by calling it through a dot operator.
What is Instance methods?
Now, suppose after knowing the process of making a Tesla model 3 car you came up with an idea that maybe you could add an Artificial Intelligence software to your car and you approached a Tesla engineer to implement your idea. So, the Tesla engineers wrote a code something like this:
Here, in the python code above we can see that our class Tesla_model_3 has a new method Jarvis which is our new AI added to the car. Also, Jarvis takes an argument that is your name to get activated. It is important to note that:
1. These kinds of methods are known as Instance methods and can be accessed only within the instance of the class.
2. Even while defining an instance method you need to use the "self" keyword as an argument.
What is the class attribute?
A class attribute is an attribute that has the same value and applies to all the instance objects created by its class. For eg. It is known to us that all the Tesla cars would have the same logo irrespective of its model. And hence we can define the attribute logo in the class Tesla_model_3 something like this:
Here in the python code, we can notice that a variable logo is defined in the class Tesla_model_3 and this variable is not inside any methods. This variable is known as a class attribute.
What is Inheritance?
Inheritance is the method of inheriting the property from its parent class to child class. Now, what is a parent class and child class? Consider an example that Tesla model 3 and Tesla Model S have the same car components only that Tesla model S had a tinted window. So, the engineers at Tesla used inheritance property of the class Tesla_model_3 to extend its attribute to the child class Tesla_model_s. Something
like this:
Here we created a new car named car1 which is a Tesla Model S. Tesla Model S is a child class of Tesla model 3 and the Tesla Model S has its own class attribute window which is tinted and has the attributes of the parent class that is Tesla_model_3. You could add different functions or methods to the child's class.
What is method overriding?
Method overriding in python is overriding the methods in the parent class by the child class. Method overriding is done by defining the same method in the child class but with its own desired properties. For eg.
In the python code above the child class Tesla_model_s, we defined a method battery_capacity but the same method was declared in the parent class Tesla_model_3. And while using the dot operator to find what is the battery capacity of Tesla_model_s it returned the value of as defined in its own class and not in the parent class. So we can say that the child class Tesla_model_s has overridden the method battery_capacity and this is known as method overriding. It is important to note that the other attributes remain the same for the child class Tesla_model_s such as the engine, body, and wheels.
What is multilevel Inheritance?
Multilevel Inheritance is the method where a child class inherits the attribute from its grandparent. For eg.
Here in the python code above, there are three classes Tesla_model_3, Tesla_model_s, and tesla_model_x, and the class tesla_model_x is the grandchild of class Tesla_model_3. As we can see that the class tesla_model_x inherited the attribute battery_capacity from its parent class Tesla_model_s and it also has all the other attribute property from the class Tesla_model_3.
What is Multiple Inheritance?
Multiple Inheritance is the method where a class is derived from more than one base class. For eg.
In the python code above the class, tesla_model_x is derived from the classes Tesla_model_3 and Tesla_model_s. But it has overridden the method battery_capacity which was declared in the parent class Tesla_model_s. This is known as multiple inheritances.
What is the super() method?
The super() method in python is a pre-defined method in python that allows explicit access to the methods in the parent class. For eg.
In the python code above, the class tesla_model_x has a super() method that returns the value of the method in the class Tesla_model_s as declared above.
What is Polymorphism?
Polymorphism is the method in which we use the same method name but with different functionality.
For eg:
In the python code above we have declared two methods with the same name but in a different class and two objects, car1 and car2 were created. When we used the dot operator to find the battery capacity of the car it returned the value of its individual class. You might ask why is the method not overriding it is because we have created two different objects from different classes.
Why do we need OOP?
OOP is required because:
1. It creates a modular structure of programs and the code looks more cleaner.
2. It makes maintaining of codebase easier.
3. It binds together the data and the function that operates on them so that no other part of the code can access this data except that function. This is known as encapsulating data.
Well, and that was it, you have learned a lot about OOP today. We can use OOP to create awesome projects in python๐. To know more about it follow me on Imagination
No comments:
Post a Comment