1
00:00:01,700 --> 00:00:03,980
Hey, welcome to the last video for today.

2
00:00:03,980 --> 00:00:09,170
Here you're going to learn about abstract classes and abstract methods.

3
00:00:09,560 --> 00:00:18,080
Yesterday, you do remember that I introduced you to a class such as Digital ticket.

4
00:00:20,810 --> 00:00:25,070
Which you'd inherit from reservation ticket.

5
00:00:27,860 --> 00:00:32,840
Yesterday we simply wrote this in a Python console just to refresh your memory.

6
00:00:33,500 --> 00:00:35,480
So it was basically something like this.

7
00:00:35,480 --> 00:00:44,570
And then I showed you that you could overwrite an existing methods of.

8
00:00:45,680 --> 00:00:47,690
The parent class.

9
00:00:48,200 --> 00:00:51,560
So generate is a method to all that parent.

10
00:00:52,460 --> 00:00:56,620
And here you could return something else which is different from that.

11
00:00:56,630 --> 00:01:03,080
So let's say hello, this is your digital ticket.

12
00:01:03,940 --> 00:01:05,140
Just an example.

13
00:01:05,260 --> 00:01:09,370
And you could, of course have more methods such as download.

14
00:01:12,700 --> 00:01:15,250
And have some codes here and so on.

15
00:01:15,520 --> 00:01:20,590
No, this is not the best practice, actually.

16
00:01:20,830 --> 00:01:32,830
A better way to do this would be to create something known as an abstract class from which other normal

17
00:01:32,830 --> 00:01:34,680
classes inherits.

18
00:01:34,690 --> 00:01:40,300
So this class would inherit from that abstract class, which we will create now.

19
00:01:40,300 --> 00:01:44,650
And the digital digital ticket class would also inherit from that class.

20
00:01:44,650 --> 00:01:48,820
So let's create that abstract class here.

21
00:01:49,480 --> 00:01:52,810
This would have a name such as Ticket in this case.

22
00:01:53,920 --> 00:01:57,820
And this would have the method generate.

23
00:02:00,340 --> 00:02:04,510
And it would have pass as code.

24
00:02:04,630 --> 00:02:11,920
And then the other classes such as reservation ticket would inherit from the ticket class.

25
00:02:12,190 --> 00:02:16,810
And same goes for digital ticket ticket.

26
00:02:18,460 --> 00:02:24,700
Now we need to add something else to the abstract class to actually make it an abstract class.

27
00:02:24,700 --> 00:02:28,630
And I'll explain you why we call this an abstract class.

28
00:02:29,080 --> 00:02:35,960
What we need to add here is a parent for this class, and the parent is ABC.

29
00:02:35,980 --> 00:02:37,210
What is ABC?

30
00:02:37,240 --> 00:02:41,620
ABC is a class which we need to import from.

31
00:02:42,700 --> 00:02:43,950
A, B, C.

32
00:02:43,960 --> 00:02:48,400
This is a module, a standard module in Python.

33
00:02:48,400 --> 00:02:54,940
You import a, b, c, A, b, C stands for Abstract Base class.

34
00:02:55,120 --> 00:02:59,080
Same goes for this A, b, c, So that's a module.

35
00:02:59,080 --> 00:03:00,250
And that's the class.

36
00:03:02,840 --> 00:03:05,140
So b c goes here.

37
00:03:05,150 --> 00:03:10,670
So now this is a child of an abstract class.

38
00:03:11,510 --> 00:03:23,410
And then in here we add the abstract methods decorator, which also should be imported from ABC import,

39
00:03:23,420 --> 00:03:27,560
ABC comma, abstract method.

40
00:03:27,560 --> 00:03:31,610
So that should be the same as this here.

41
00:03:31,820 --> 00:03:39,110
Now, what's special about this class is that if you let me try to show you that to you in the Python

42
00:03:39,110 --> 00:03:39,740
console.

43
00:03:40,970 --> 00:03:51,710
So let's import that and then let's define this class or you can import it, but I'll just copy and

44
00:03:51,710 --> 00:03:52,760
paste it here.

45
00:03:54,140 --> 00:04:01,700
And then if I try to create an instance out of a ticket class, this class, right, I am instantiating

46
00:04:01,700 --> 00:04:02,150
it.

47
00:04:02,540 --> 00:04:05,240
If you try to create an instance, you will get an error.

48
00:04:05,780 --> 00:04:07,580
The error is very specific.

49
00:04:07,580 --> 00:04:15,410
It says can't instantiate abstract class ticket with abstract method generate.

50
00:04:15,410 --> 00:04:23,800
So you can not instantiate an abstract class because an abstract class is an abstract class.

51
00:04:23,810 --> 00:04:34,610
So it's a class which is not supposed to create instances is something abstract, and its role is to

52
00:04:34,610 --> 00:04:36,590
define a structure.

53
00:04:36,590 --> 00:04:37,100
Basically.

54
00:04:37,100 --> 00:04:43,280
It's not a must to use abstract abstract classes, but it makes your code more clear.

55
00:04:43,280 --> 00:04:52,010
So you see, for example, now that, oh, this reservation tickets inherits from this ticket class.

56
00:04:52,040 --> 00:04:58,190
Oh, so you say, let's see what what the ticket class is and you see here, then you see that this

57
00:04:58,190 --> 00:04:59,900
has this generate method.

58
00:05:01,250 --> 00:05:10,760
And then you realize that every child inheriting from that class should implement a generate method.

59
00:05:11,850 --> 00:05:14,050
So that's a must now.

60
00:05:14,070 --> 00:05:18,210
So that way you basically are trying to define some rules.

61
00:05:18,210 --> 00:05:21,480
Perhaps you are collaborating with other programmers.

62
00:05:21,480 --> 00:05:28,200
So that makes it very clear that all the ticket classes, including this one in here, which also is

63
00:05:28,200 --> 00:05:36,220
inheriting from that abstract class, this also should have a generate method.

64
00:05:36,240 --> 00:05:44,220
So this way you are forcing the programmers, which could be you or another collaborator to create to

65
00:05:44,220 --> 00:05:52,710
implement a generate method for each class, because that is a must if the programmer doesn't implement

66
00:05:52,710 --> 00:05:53,340
a methods.

67
00:05:53,340 --> 00:06:01,920
For example, let's try to create a PDF ticket here which inherits from the ticket abstract class,

68
00:06:02,280 --> 00:06:08,130
and this has a generate PDF methods.

69
00:06:08,850 --> 00:06:13,500
Let's pass for now and it doesn't have a generate method.

70
00:06:13,980 --> 00:06:19,770
In that case, when you try to instantiate PDF ticket you're going to get an error.

71
00:06:19,950 --> 00:06:25,980
So this will not work and that means it will force the programmer to actually remind themselves that

72
00:06:25,980 --> 00:06:28,610
they need to add a generate method.

73
00:06:28,620 --> 00:06:37,530
So it's good to use abstract classes when you see that it's a must that all those classes should have

74
00:06:37,530 --> 00:06:39,330
a particular method.

75
00:06:40,930 --> 00:06:47,590
That was about abstract classes and abstract methods such as this one in here.

76
00:06:48,460 --> 00:06:55,630
Yet with that you have a very complete overview of classes and advanced concepts of both classes.

77
00:06:55,900 --> 00:07:01,780
As we move through the course, we'll have more real world scenarios when we use classes.

78
00:07:01,780 --> 00:07:05,980
So basically most of the apps will be building from now on.

79
00:07:06,640 --> 00:07:11,770
We'll have an object oriented programming structure using classes.

80
00:07:11,890 --> 00:07:17,890
That's why I introduced you to object oriented programming during these last few days, so that now

81
00:07:17,890 --> 00:07:23,370
you're prepared to actually practice classes in more real world scenarios.

82
00:07:23,380 --> 00:07:25,180
So thanks a lot for following so far.

83
00:07:25,180 --> 00:07:29,560
And please go to the next day and I'll see you there with building some more programs.

84
00:07:29,560 --> 00:07:30,190
Thanks.

