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