hey guys welcome to the next blog in Python tutorial for beginners in this blog we will talk about creating abstract classes in Python so let's get started so to start with I have here two classes one is a shape class another is a square class in shape class I have to empty methods one is area and other is perimeter and I'm using shape as a superclass and square as a subclass and square class is inheriting from the shape class now in this example I want to do two things one is I don't want to allow the other users to create an instance of a shape class because shape class acts like a template for the square class so I don't want to allow those users to create an instance of this shape class so let's see right now if we can create an instance of this shape class or not so I'm going to just right shape is equal to shape and when I run the code my code runs fine that means right now I'm able to create an instance of the shape class which I don't want the second thing is I want to make sure that both these methods which are there inside the shape class are implemented inside the square class so I want to make sure that whoever is using the shape class he or she must implement these two methods inside the subclass so how can I achieve these two things now it turns out that abstract classes are just made for that so how to create an abstract class in Python the fact is Python on its own doesn't provide any abstract class but don't worry there is a built-in module in Python which we can use to create an abstract class so let's import this built-in module first so I'm going to just write from ABC import capital ABC comma abstract math so these are the two things we need to import from this module now this module ABC stands for abstract base classes and this allows us to create the abstract classes so how we can create an abstract class using this ABC module so what we need to do here is we need to inherit from the ABC module into the shape class okay so shape class is inheriting from the ABC module now once this shape class inherit from the ABC module the second thing what we need to do here is we need to add a decorator here and how to add a decorator we just need to use at the rate and then the name of this abstract method which is like this and let me use this abstract method decorator on top of this parameter method also so what this abstract method decorator does is it makes these two methods abstract and what is an abstract method an abstract method is a method which you must implement in the subclass so when you run the code now it will give us this error which says can't instantiate abstract class shape with the abstract method so once you create any of your class method as abstract then your class becomes the effect class and then you cannot instantiate this class like this so our first goal is achieved that we cannot instantiate this shaped last now because this class is now an abstract class and how to create an abstract class you just need to import the ABC module and then you just need to add this decorator on top of your method and if you do this even with one method in your class then your class becomes abstract so now we have removed the instantiation of this shape class but now when I try to instantiate this square object from the square class let's see what happens so this square class takes one argument which is the side of the square and let me run this code and once again it gives us an error and this error says can't instantiate abstract class square with abstract methods so that means that whenever you inherit from a class which is an abstract class and if this class contain any abstract methods then the subclass have to implement the methods which are abstract so this method area is an abstract method and this method parameter is also an abstract method that means we have to provide the implementation of these two method inside our subclass so I'm going to provide the implementation of area first of all into the subclass and area I'm going to just return as the multiplication of the sides right so self dot underscore underscore side multiplied by itself so self dot underscore underscore side and let's say for now I will remove this abstract decorator from the parameter method and let's run the code once again and you can see it runs fine so there is no error when we instantiate this class when we implement all the abstract method into your subclass so right now there is only one abstract method which is area into the shape class which we need to implement in the subclass and that we have done so we'd see no error here now let's add this decorator abstract method on top of this parameter method also and once again when I run the code it will give me once again this error it's going to give me the same error which says can't instantiate the abstract class square with the abstract method parameter so we also need now to implement this method also so let's just implement this parameter method inside the subclass once again and what is the parameter it's four times the size of your square so self dot underscore underscore size and now we have implemented both of them and once again when we run the code it runs fine let's also try to print the area and perimeter of the square so I'm going to just use this object to call the area method and in the next line I'm going to just use print to once again call this object and this time I will call the parameter method and let's run the code once again and it prints the area of the square and the perimeter of this square so let's rewind what we have learned till now so in Python you can define an abstract class by importing this module and this abstract method and this ABC stands for abstract base classes and you need to inherit from this ABC into the class which you want to make abstract the second thing which you need to do is you need to provide this decorator at the rate abstract method on top of at least one of the methods inside your abstract class that makes your class abstract and what are the properties of the abstract class you cannot instantiate an abstract class so you cannot create an object of this abstract class and you need to implement all the abstract method into the sub class which is inheriting the abstract class so this abstract class acts like just a template for other classes which wants to inherit from this shape class so this is how you can use abstract classes in Python I hope you have enjoyed this blog and I will see you in the next blog
Post a Comment