Hey, welcome to the last video for today.
Here you're going to learn about abstract classes and abstract methods.
Yesterday, you do remember that I introduced you to a class such as Digital ticket.
Which you'd inherit from reservation ticket.
Yesterday we simply wrote this in a Python console just to refresh your memory.
So it was basically something like this.
And then I showed you that you could overwrite an existing methods of.
The parent class.
So generate is a method to all that parent.
And here you could return something else which is different from that.
So let's say hello, this is your digital ticket.
Just an example.
And you could, of course have more methods such as download.
And have some codes here and so on.
No, this is not the best practice, actually.
A better way to do this would be to create something known as an abstract class from which other normal
classes inherits.
So this class would inherit from that abstract class, which we will create now.
And the digital digital ticket class would also inherit from that class.
So let's create that abstract class here.
This would have a name such as Ticket in this case.
And this would have the method generate.
And it would have pass as code.
And then the other classes such as reservation ticket would inherit from the ticket class.
And same goes for digital ticket ticket.
Now we need to add something else to the abstract class to actually make it an abstract class.
And I'll explain you why we call this an abstract class.
What we need to add here is a parent for this class, and the parent is ABC.
What is ABC?
ABC is a class which we need to import from.
A, B, C.
This is a module, a standard module in Python.
You import a, b, c, A, b, C stands for Abstract Base class.
Same goes for this A, b, c, So that's a module.
And that's the class.
So b c goes here.
So now this is a child of an abstract class.
And then in here we add the abstract methods decorator, which also should be imported from ABC import,
ABC comma, abstract method.
So that should be the same as this here.
Now, what's special about this class is that if you let me try to show you that to you in the Python
console.
So let's import that and then let's define this class or you can import it, but I'll just copy and
paste it here.
And then if I try to create an instance out of a ticket class, this class, right, I am instantiating
it.
If you try to create an instance, you will get an error.
The error is very specific.
It says can't instantiate abstract class ticket with abstract method generate.
So you can not instantiate an abstract class because an abstract class is an abstract class.
So it's a class which is not supposed to create instances is something abstract, and its role is to
define a structure.
Basically.
It's not a must to use abstract abstract classes, but it makes your code more clear.
So you see, for example, now that, oh, this reservation tickets inherits from this ticket class.
Oh, so you say, let's see what what the ticket class is and you see here, then you see that this
has this generate method.
And then you realize that every child inheriting from that class should implement a generate method.
So that's a must now.
So that way you basically are trying to define some rules.
Perhaps you are collaborating with other programmers.
So that makes it very clear that all the ticket classes, including this one in here, which also is
inheriting from that abstract class, this also should have a generate method.
So this way you are forcing the programmers, which could be you or another collaborator to create to
implement a generate method for each class, because that is a must if the programmer doesn't implement
a methods.
For example, let's try to create a PDF ticket here which inherits from the ticket abstract class,
and this has a generate PDF methods.
Let's pass for now and it doesn't have a generate method.
In that case, when you try to instantiate PDF ticket you're going to get an error.
So this will not work and that means it will force the programmer to actually remind themselves that
they need to add a generate method.
So it's good to use abstract classes when you see that it's a must that all those classes should have
a particular method.
That was about abstract classes and abstract methods such as this one in here.
Yet with that you have a very complete overview of classes and advanced concepts of both classes.
As we move through the course, we'll have more real world scenarios when we use classes.
So basically most of the apps will be building from now on.
We'll have an object oriented programming structure using classes.
That's why I introduced you to object oriented programming during these last few days, so that now
you're prepared to actually practice classes in more real world scenarios.
So thanks a lot for following so far.
And please go to the next day and I'll see you there with building some more programs.
Thanks.