0
1
00:00:01,290 --> 00:00:08,460
Let's begin the chapter by looking at inheritance.
1

2
00:00:08,550 --> 00:00:11,620
And let’s begin by looking at motivation for the need for inheritance.
2

3
00:00:11,620 --> 00:00:12,200
Now,  
3

4
00:00:12,210 --> 00:00:17,210
in our case study, we looked at these different users and their capabilities
4

5
00:00:17,370 --> 00:00:20,650
So, each user is basically a class.
5

6
00:00:20,820 --> 00:00:25,290
So, in this case, can you see any issue with this design?
6

7
00:00:26,040 --> 00:00:31,860
You may notice that the methods emphasized in red are exactly identical in what they do
7

8
00:00:31,890 --> 00:00:37,800
, i.e., all users can bookmark any item and then can also rate any bookmark.
8

9
00:00:38,370 --> 00:00:40,560
So, what does that mean. 
9

10
00:00:40,620 --> 00:00:48,110
It means duplicate code and duplicate code implies maintenance nightmare, i.e., change in one method 
10

11
00:00:48,280 --> 00:00:54,670
will need to be updated in all four of the classes. And what is the solution for this. 
11

12
00:00:55,160 --> 00:00:57,480
As you must have guessed, it is inheritance!
12

13
00:00:57,810 --> 00:00:59,730
So, let's see what it means.
13

14
00:00:59,750 --> 00:01:05,790
Also, just note that postAReview method is not common to all of the classes as a review written by 
14

15
00:01:05,790 --> 00:01:11,620
a regular user needs approval from a content editor in order to be published on the Website.
15

16
00:01:12,080 --> 00:01:16,930
We know that it is not required for other users who are staff members
16

17
00:01:18,300 --> 00:01:24,460
Inheritance is one of the fundamental features of object-oriented programming.
17

18
00:01:25,600 --> 00:01:30,300
And here is how the new design looks like after applying inheritance.
18

19
00:01:30,330 --> 00:01:37,260
Basically, what we do is we abstract out all the common features and put them only in User class,
19

20
00:01:38,150 --> 00:01:42,350
i.e., we remove all the duplicate code and move it into User class.
20

21
00:01:42,390 --> 00:01:49,050
You can see that all the common methods are now in User. User class will also include any methods that
21

22
00:01:49,050 --> 00:01:50,330
are unique to itself
22

23
00:01:50,520 --> 00:01:56,940
like the postAReview method. The remaining classes are now linked to User class via inheritance.
23

24
00:01:56,940 --> 00:01:57,740
.
24

25
00:01:57,840 --> 00:02:04,380
So, it is a special type of relation and we now call the User class as a super class and the three classes
25

26
00:02:04,500 --> 00:02:05,600
linking to it 
26

27
00:02:05,640 --> 00:02:13,100
via inheritance as subclasses. A general phrase used to refer to such a design with
27

28
00:02:13,170 --> 00:02:21,210
inheritance relationship is inheritance tree or inheritance hierarchy. As the name inheritance suggests, subclasses
28

29
00:02:21,480 --> 00:02:28,350
inherit features from super classes; and features are basically members, i.e., variables and methods
29

30
00:02:28,410 --> 00:02:33,390
defined in superclass are now automatically part of subclasses.
30

31
00:02:33,600 --> 00:02:40,370
So, it is as if those methods and variables are declared in the subclasses itself. In fact, subclasses 
31

32
00:02:40,530 --> 00:02:43,240
can access superclass members directly.
32

33
00:02:43,380 --> 00:02:50,370
For example, EmailAdmin can directly invoke saveWebLink method defined in User without any
33

34
00:02:50,370 --> 00:02:58,110
special operator. Note that though subclass can access superclass members, a superclass can never access members
34

35
00:02:58,110 --> 00:03:00,270
of a subclass.
35

36
00:03:00,270 --> 00:03:07,020
With this new design, we can now say that EmailAdmin inherits from User, Editor inherits from User
36

37
00:03:07,530 --> 00:03:10,960
and also ChiefEditor inherits from User.
37

38
00:03:10,970 --> 00:03:16,660
Note that this inheriting of members applies to only non-private members of superclass,
38

39
00:03:16,980 --> 00:03:21,860
but let’s not worry about accessibility for now and it will be discussed later.
39

40
00:03:21,960 --> 00:03:28,070
So, basically we don’t have duplicate code anymore as it is moved nicely into one single location
40

41
00:03:28,110 --> 00:03:34,150
called a superclass and it is automatically available to all its subclasses.
41

42
00:03:34,170 --> 00:03:39,990
In other words, we get the benefit of code re-use due to inheritance.
42

43
00:03:39,990 --> 00:03:46,520
Now if we have to make a code change, we do it only at one place, which is the superclass.
43

44
00:03:46,530 --> 00:03:52,410
Also, if superclass acquires new methods, then you can simply compile the modified
44

45
00:03:52,410 --> 00:03:58,140
superclass and replace the old .class file with this newer one and with that all
45

46
00:03:58,140 --> 00:04:01,300
all subclasses would have those new methods too,
46

47
00:04:01,590 --> 00:04:09,210
i.e., you don’t even have to touch your subclasses. Subclasses are basically specialized versions of
47

48
00:04:09,210 --> 00:04:10,630
superclasses.
48

49
00:04:10,950 --> 00:04:16,440
i.e., they can do whatever their super classes do and in addition can also do their own unique stuff
49

50
00:04:16,440 --> 00:04:17,280
.
50

51
00:04:17,310 --> 00:04:24,030
In other words, in addition to inheriting superclass features, they can also add their own members like
51

52
00:04:24,040 --> 00:04:30,100
the approveReview method or the handleEmailCampaign method or even any new fields.
52

53
00:04:30,660 --> 00:04:35,950
In addition to adding their own members, they can also override superclass methods
53

54
00:04:36,120 --> 00:04:42,730
thus redefining behavior, i.e., they can change or extend the superclass’s behavior.
54

55
00:04:42,960 --> 00:04:49,800
For example, postAReview method from User class is also inherited by subclasses, but postAReview method
55

56
00:04:49,800 --> 00:04:52,640
in subclasses work differently,
56

57
00:04:52,740 --> 00:04:57,780
i.e., when a review is posted by EmailAdmin or an Editor or a ChiefEditor,
57

58
00:04:57,780 --> 00:05:01,560
then that review requires no special approval, which is not the case
58

59
00:05:01,590 --> 00:05:06,720
with postAReview method in User. So, we need to override that behavior
59

60
00:05:06,870 --> 00:05:14,700
and that’s precisely what postAReview method in subclasses are doing. So, subclass capability would
60

61
00:05:14,730 --> 00:05:21,370
would include superclass capabilities as well as the capabilities defined in its own class.
61

62
00:05:21,720 --> 00:05:25,990
Note that sometimes a superclass is also referred to as supertype.
62

63
00:05:26,430 --> 00:05:29,840
Sometimes it is also referred to as a base class.
63

64
00:05:29,850 --> 00:05:37,260
Similarly, a subclass is also referred to as a subtype or sometimes as a derived class as it is derived
64

65
00:05:37,260 --> 00:05:40,350
from a base class.
65

66
00:05:40,350 --> 00:05:46,830
So, we have this new design after incorporating inheritance. But, can you spot any more redundancies here
66

67
00:05:46,830 --> 00:05:47,340
,
67

68
00:05:47,460 --> 00:05:50,670
i.e., do you think some more inheritance is possible here?
68

69
00:05:50,820 --> 00:05:56,640
You may notice that postAReview method is identical across all subclasses.
69

70
00:05:56,670 --> 00:06:04,560
Moreover, approveReview & rejectReview methods are common to Editor and ChiefEditor classes. So, we can
70

71
00:06:04,560 --> 00:06:07,740
apply inheritance across subclasses
71

72
00:06:08,660 --> 00:06:16,020
And here is the new design. postAReview method has been moved into a new superclass called Staff, which will
72

73
00:06:16,020 --> 00:06:18,940
now be a subclass of User.
73

74
00:06:19,020 --> 00:06:25,260
We have also made ChiefEditor a subclass of Editor and the two common methods approveReview & rejectReview
74

75
00:06:25,260 --> 00:06:27,870
are moved into Editor.
75

76
00:06:28,020 --> 00:06:29,510
So that's the final design.
76

77
00:06:29,700 --> 00:06:33,430
and the inheritance tree is now three levels deep.
77

78
00:06:33,720 --> 00:06:40,530
Note that a class would inherit members from all its super classes in the hierarchy, i.e., inheriting
78

79
00:06:40,590 --> 00:06:47,940
members is not restricted to only its direct superclass. For example, ChiefEditor would inherit members
79

80
00:06:47,940 --> 00:06:48,980
from Editor,
80

81
00:06:48,990 --> 00:06:51,250
Staff, and also User.
81

82
00:06:51,710 --> 00:06:58,650
However, it doesn’t inherit from EmailAdmin as EmailAdmin is in a separate branch and so is not one
82

83
00:06:58,650 --> 00:07:00,710
of its superclasses.
83

84
00:07:00,930 --> 00:07:07,140
So, we have seen how inheritance has helped us to remove duplicate code. There are few more very
84

85
00:07:07,140 --> 00:07:11,750
exotic advantages of inheritance and we will look at them soon.
85

86
00:07:12,120 --> 00:07:14,900
So, we looked at inheritance from a design standpoint. 
86

87
00:07:14,910 --> 00:07:19,650
Next, let's see how this can be translated into code.
87

88
00:07:19,810 --> 00:07:25,460
Now every object-oriented programming language has its own way of implementing inheritance.
88

89
00:07:25,650 --> 00:07:32,700
In java, keyword called “extends” is used in the class declaration to build an inheritance relationship
89

90
00:07:32,700 --> 00:07:33,180
,
90

91
00:07:33,600 --> 00:07:39,430
i.e., a subclass would use the keyword extend to inherit from a superclass.
91

92
00:07:39,470 --> 00:07:42,960
Here are the class declarations for our classes.
92

93
00:07:42,990 --> 00:07:50,040
User class is at the topmost level and hence does not use “extends” keyword. However, Staff is a subclass
93

94
00:07:50,040 --> 00:07:56,010
of User and hence its declaration includes the keyword “extends” followed by the superclass name, which
94

95
00:07:56,010 --> 00:07:57,300
is User.
95

96
00:07:57,300 --> 00:08:04,620
Similarly, EmailAdmin & Editor extend Staff while ChiefEditor extends Editor
96

97
00:08:05,130 --> 00:08:09,380
Note that a class can extend from only one class.
97

98
00:08:09,540 --> 00:08:11,070
So that's the rule!!
98

99
00:08:11,610 --> 00:08:12,820
So, that’s about it
99

100
00:08:12,860 --> 00:08:16,470
and we have seen how inheritance is implemented.
100

101
00:08:16,470 --> 00:08:22,550
Next, we will learn about some really important stuff related to inheritance and we will also be testing
101

102
00:08:22,650 --> 00:08:25,670
all of the inheritance stuff by writing some code.
102

103
00:08:25,980 --> 00:08:26,430
Thank you.
