hey guys welcome to the next blog on Python tutorial for beginners now before starting our object oriented programming journey in Python let's discuss about what is the difference between the procedural programming and the object-oriented programming so you will have the better idea why we use object-oriented programming now traditional programming languages such as C or Pascal were called procedural programming languages or structural programming languages where the basic unit was functions now programming in these type of procedural languages involves choosing a data structure and then designing the algorithm and then translating that algorithm into a code so if this sounds little bit confusing let me try to explain it with an example so let's say you have been given a task to create a program for a passenger who wants to travel from one place to the another place using a cab service so if we think from the point of view of procedural programming what we do in the procedural programming is we create some global data structure which holds the data so here for example we create some kind of data structure which can hold the data for example in case of a cab service which cab service is it or which type of cab is it and at what location this cab is standing all these kind of data we store in a desta structure in our global environment now after storing the data we design an algorithm so let's see what kind of algorithm we can develop in the procedural programming language for our cab booking service so this is a pseudocode which i have written so first of all the passenger will open the app from which he can book a cab and then he will book the cab and once the cab is booked he will wait for the cab and then once cab arrives he will sit in the cab and then he will reach to his or her destination and at the end he will pay the fare of the cab and this is the pseudocode of that algorithm and then we will translate this algorithm into actual code in procedural programming language now in these type of procedural programming languages we construct rate on creating the functions and the major drawback of using these functions is that data and operations on the data are separated that means we need a methodology to send this data to these functions so here we need to send this data which we have saved globally into these functions and these functions take this data either as argument or as a global variable and then perform some actions on this data and give you some result now these kind of functions are passive what do I mean by passive here that is these kind of function cannot hold any information inside them so once you give the data they are able to give you the result back after performing some operations but they cannot save or hold the state or the data so that if you want to use that data in some other place in your code then it will be very difficult using these kind of functions which you use in procedural programming now let's look at the object-oriented approach of doing things so in object-oriented programming languages like C++ or Java or Python the basic unit is class now if we take the same example of a passenger who wants to travel from one place to another using a cab service you using object-oriented programming thinking which depends upon the creation of object we can create different kind of object for example for a cab we can create a class called cab and then we can create a class for cab driver and the third class we can create for a passenger okay so a class you can create for any real-life object it can be a car it can be a motorbike it can be a book or employee or a person so object-oriented programming allows us to create object so first of all what is a class so a class refers to a blueprint in which we can have data and methods okay so for example for our cab class what attributes this cab class can have for example a cab service which cab service we want to take what is the make of the cab is it a Toyota or a BMW or a fox wagon cab at which location this cab is right now what is the number plate of that cab so the passenger can recognize this cab so all these things which I have written here are called data because they can hold some kind of data number plate has number plate data location have geolocation data may have the make data cap service can have data like uber or any other cab service and the other thing which at last can have are called method so earlier we have seen that we can create functions and when these functions you use inside a class they are called method ok so functions inside a class are called method now the data inside this object or class is called attributes or the member variables which can hold some data and using this class we can create object of the cab which means we can create different object using a same class and how to create object using classes we will see in the next blog in the real-life example so don't worry if you don't understand how these things works I will give you a real-life example so you will be able to understand in a better way now what is an object an object is a software unit that combines data and methods okay so we have this data here and then we have the methods inside the class and object is able to combine both of them which is data with the methods now these object for example a cab object and the passenger object can exchange the data between them also so data is interchangeable between for example the passenger object and a cab object so let's rewind once again what we have learned about object-oriented programming so the basic unit in object-oriented programming is a class and at last refers to a blueprint which can have the data and methods now using a class we can create objects and what is the object object is an instance of a class and each object can have its own data and method and an object is able to store the state of some kind so at which location this cab is right now so this is a state and an object is able to store at that state now in procedural programming if you remember there is no relation between the data and the method right we need to provide the data to the method which are separate entities in the procedural programming language now these data members are called attributes or member variables and these functions which you define inside a class are called methods and what are some of the key differences between procedural programming language and object-oriented programming language the first is the unit in procedural programming language is function and on the other hand in object-oriented programming the unit is class the second is the procedural programming concentrate on creating functions while object-oriented programming starts from isolating classes and then they can have data and methods inside it in procedural programming language the data and the functions are separate and in object-oriented programming language data and methods are not separate they are on the part of a single object of Atlas now if all this seems to be little confusing to you don't worry you are not alone and I will try to solve this confusion in the next blog in which I will tell you how to create the classes and how to use classes in Python so stay tuned and please keep reading these blog I will see you in the next blog
Post a Comment