Abstract Class and Abstract Method in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens in this video we'll talk about abstract classes and abstract methods in Python now first of all Python by default don't support abstract classes you will not see this support indirectly but we have a module which is ABC model which we can use here to achieve abstract classes now ABC stands for abstract base classes or some name right so we can use that to create our own abstract classes now how do we do that it's very simple let's open PyCharm in fact the thing is we have to answer two questions here the first one is what is abstract class and why do we need it so we start with what and then we'll discuss why we need it okay so what is that flat class so basically let's create a class here and we can create a class name let's say computer and of course computer can have multiple methods or multiple functions here and I will say okay that's a computer class and in this class I can create some methods now how do we create method is very simple we say def and then let's create a method which is let's say versus as a computed process right and then I can print here I can say it running find nothing fancy let's keep it simple and VCR saying running here so that's how you could applause and that's how you get a method now how do you make a method so a method has a name method has a at by the parameters which X it accepts or arguments and then method also has a body so this body here this print statement is a body right so this body can have multiple statements we can say we can do some processing there we can take values we can take variables and we can do some operations but then just to keep it simple this method has only one statement now whenever you your method has a body it's a normal method so and normal methods are the in normal classes but what if I don't want to mention anything inside this method I want to keep it blank I do I just want to declare this method now it is a declaratively means it is it doesn't have a body right now the way you can declare a method is by saying paths so when you say pass it then it simply means I don't have anything inside this in this method now this methods which only has a declaration but not definition we call them as abstract methods right and then a class which will have abstract methods is called abstract classes but then this type of concept is there from in other language as well it is reading Java in c-sharp but in panel it's been different right because python by default isn't does not support abstract classes now if you want to make an abstract class you need to do something and that something is because there's one more definition there is one more thing for abstract class you cannot create a object of it example if I try to get object of computer here and okay and if I say comm dot process you can see there is no error at least I did I act when I am typing this if I have this code it perfectly works there is no output of course because we are saying pass the thing is we are able to get this object because this is not an abstract class yet how do you make it abstract class and for that you have to import a module so which one is we have to import so you have to import ABC and in this you have to import only two things one is ABC which is abstract base classes and you need to also import the method abstract method so this will make this class as a subclass of ABC so that this also becomes abstract class so now this is abstract class and then this method has to be abstract method so we say so will be the decorator here which is a great abstract method now using decorator can mention hey this process is a method which is abstract so you are talking to a compiler about it now once you do this and if you run this code now you can see we caught an error it simply says that instantiate abstract class that that's important now filming after class abstract you can't great object of it because because we have a method which is which is not defined now that's something have to remember now the can create multiple classes here example I can create one no class of course I can't get object of or company because it's an abstract class and has a method which is undefined which is declared so what we can do is we can create a new class which is laptop because when you say computer computer is just a fancy term right because we don't buy computers we buy laptop we buy the extra we buy phones so this laptop is actually a subclass of a computer and then here I can define that method but what happens if I don't define methods now in this case if I try to create object of laptop so you can see if I say in laptop what do you think will it work so before continuing you can pause the video and you can answer in the comment section what do you think will it work or not because this time I am NOT getting object of computer I am getting object of laptop will it work and then I'm later I'm not calling anything is just I'm creating the object ones right now and let's run I hope you have answered and you can see we are getting either the other is can't instantiate abstract class laptop with abstract methods process now that that's a thing laptop is inheriting computer in which you have a abstract method so it is compulsion for you to define that method otherwise even this class which is laptop is abstract class so in this case I will simply say def and I will define the method and then I will say process it's running that's it nothing fancy will this work let's try now because now I'm defining that method so if I say run and it worked it's just that I'm not following this process that's why you don't have any output and you can do that we can say compound dot process at least I can see some output in the console and record the output that's great so this is working so you can see we can get a class and that class will implement all the methods right that's that's important and if you fail to implement all the methods all the abstract methods you will get an error now this is what extra class is abstract class will have at least one abstract method that's important right okay now this is good this is something you will say hey okay this is the concept and he's working but what's the use of this now think about this whatever concept you have learnt till now everything makes sense why do you know how to use loops you know what these variables are and we can use them in the real life but what about abstract laws because see if you if you look at these two statements we we are calling a method which is process and then this process is getting called from laptop so what is important here is a laptop plus what do you think is a use of this popular class anyway so even if I remove that it will not make any difference right I can simply this glass I can remove this computer as extends and it was it was surely works see this thing makes sense only when your class computer has some extra methods which laptops needs right what if you have two methods here or three methods here and laptop is taking those methods directly then it makes sense to use a computer but what if you your class only has abstract methods does it make any sense so the answer is yes it makes sense only when you are designing an application in a UPS way so basically when you follow the concept of object-oriented programming you have to follow some patterns and one of the pattern is let's say if you have out of classes example let's say if you talk about a programmer so let me get up in a class for programmers here just to understand the concept let's say class programmer let's only one method so I will say def work because L programmer we do work right and then in this I can say now okay what programmer does right is it we write code yes we do write code but we also saw the problem site so we say solving okay we do we solve problems what we do is all paths like that that's why we spend by example time right ok so as a programmer we do that and let me create an object of a programmer here I say plug 1 equal to programmer and once we got the object I can simply say prog 1 dot work and of course this should print something this with it should print solving bubs it should it will print H running because that's what we are doing in process now what's important is as a programmer you need a machine to work with right of course you can't walk on paper like programmer write codes on or laptop on mobile phone on desktop so of course you you need a computer here now what do you think of what type of object I will pass here will it be a type of laptop will it be a type of computer so what we can do is let me pass a computer type so which is also calm now this calm can be anything this can be a laptop this can be a tech stock right all it can be anything it can be a mobile phone or it can be a board whiteboard Oh will that work now can I say can I pass an object of a whiteboard here so let me create a class whiteboard I mean just to see if that works I will say whiteboard and of course whiteboard will have certain methods of course it will it will not tell them either which is process but at least we can have a method like and then what there's one more thing in this print I will also do something with with calm I will say palm dot process right and then this vibe would have a method which is let's say right that's what we do on boards right and then here I will say it's writing it's okay what's that okay so now because a white book class as well and the thing is this white board can I simply pass an object of white board here say in this work I have to pass an object of if I pass the object of laptop there's no problem with if you can see if I run this code and let me not call the process again let me only call work and if you say it on this code it works because it says solving buffs and it is running so it is solving bugs because we are calling work it is printing it's running because from this method we are calling process because as a programmer we write a code and it you have to say process right but what if if I create an object of a whiteboard I will say calm to is equal to white bowl and if I pass an object of white word you can see there is no compile time issue I hope they yeah there is no compile time issue but if I have this code you can see we caught this show because white board don't know about the witches process now why doesn't have a process method because white board there is no compulsion for a white board to implement or to have that method which is process right and if you say hey white board is a computer in this case it becomes compulsion for this white board to have this method of just process so that's the design expert or I guess not that you can't write up code with other abstract classes but then sometime it's also depend upon the design the way you design your application of the way you design your classes so this is one way of defining a class right you can get abstract class so that the other classes will be we'll be having some signature or some restriction to words method define example when you define API so you can create a API if someone wants to use your API they have defined all the methods right so that's how that's how we use abstract classes that's how we use abstract methods and it's not that you can only have one abstract whether you can have multiple abstract methods and you can also have normal methods there so I hope you got the idea but I've shot glass just to give you a quick recap abstract class has at least one abstract method in a class what is abstract method abstract methods are methods which doesn't have a definition it only has a declaration and if you want to use them in Python since Python by default does not support them you need to import an module which is ABC ABC stands for abstract base classes so that's it from this video I hope you enjoyed or just to see just to remove this classes that it will work before ending it before I did you remove this whiteboard as well and then you can also try it with desktop so you can create a class desktop the only thing is the way you have a laptop here of course is a programmer you can't say hey we only work on laptop you can also work on next top so you can create a desktop class and desktop class will also have some methods which is process of course and the stop class will extend and the extra class will also inherit computer and then here you can get object of the Ixtapa it will work so that's it from this video I hope you enjoyed it too in the comment section and do subscribe for further videos bye bye
Info
Channel: Telusko
Views: 181,094
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: UDmJGvM-OUw
Channel Id: undefined
Length: 12min 28sec (748 seconds)
Published: Fri Feb 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.