1

00:00:01,080  -->  00:00:02,390
Hi there.

2

00:00:02,610  -->  00:00:08,090
So this chapter is all about inheritance and we have learned a great deal about it.

3

00:00:08,220  -->  00:00:14,460
We know that with inheritance we not only inherit functionality but also extend on all the existing

4

00:00:14,470  -->  00:00:21,870
behavior defined in the super to us non-Nazi how we can do the opposite which is to prevent inheritance

5

00:00:21,890  -->  00:00:22,310
.

6

00:00:22,800  -->  00:00:28,260
So there can be instances where you want to prevent inheritance that is you want to a class to be the

7

00:00:28,260  -->  00:00:29,860
final version.

8

00:00:30,030  -->  00:00:34,890
So let's see why you would want to do this on also how it can be done.

9

00:00:34,920  -->  00:00:40,470
One reason why you want to prevent inheritance is that you do not want your methods to be overridden

10

00:00:40,470  -->  00:00:41,150
.

11

00:00:41,430  -->  00:00:47,430
That's because you want them to work in the exact same way as you have implemented them.

12

00:00:47,450  -->  00:00:53,160
However sometimes it's not just about method or writing but you do not want any subprocess to be created

13

00:00:53,590  -->  00:00:56,980
as they may affect the semantics of your class.

14

00:00:57,030  -->  00:01:03,350
In other words you do not want the invariants that are defined by your class to be compromised by subprocess

15

00:01:03,360  -->  00:01:03,960
.

16

00:01:04,170  -->  00:01:10,440
For example we know string classes immutable and the designers did not want subclasses of string to

17

00:01:10,440  -->  00:01:14,570
be created as they could compromise this immutable behavior.

18

00:01:14,700  -->  00:01:21,300
So the String class and just this by preventing inheritance altogether in fact item 15 in Effective

19

00:01:21,300  -->  00:01:27,690
Java recommends minimizing notability off classes as much as possible on one of the rules and doing

20

00:01:27,690  -->  00:01:30,210
it is to prevent inheritance.

21

00:01:30,210  -->  00:01:32,920
Item 15 is pretty important on its own point.

22

00:01:32,940  -->  00:01:37,100
I tried to include it as a separate lesson.

23

00:01:37,110  -->  00:01:38,870
Now there are two ways to prevent inheritance.

24

00:01:38,970  -->  00:01:43,270
One is by marking the classes final while the other is by using a private constructor.

25

00:01:43,500  -->  00:01:46,080
Let's look at both of them.

26

00:01:46,350  -->  00:01:51,140
We already look good using the final keyword with both variables and methods.

27

00:01:51,150  -->  00:01:55,530
We said that if Final is used with a variable then its value can never be changed.

28

00:01:55,740  -->  00:02:01,430
If final is used with a specific method then that method can never be overridden.

29

00:02:01,440  -->  00:02:05,100
Similarly a final key word is used in class declaration.

30

00:02:05,220  -->  00:02:08,360
Then the class can never be extended.

31

00:02:08,370  -->  00:02:13,340
It also means that none of its methods can ever be overridden.

32

00:02:13,380  -->  00:02:20,730
So essentially final implies that something is final or whatever however final process intangible.

33

00:02:21,270  -->  00:02:24,510
So it is not expendable but it is still intangible.

34

00:02:24,510  -->  00:02:26,970
And one good example is the string.

35

00:02:27,180  -->  00:02:31,500
If you look at it source code you will also see that it's class declaration includes the final keyword

36

00:02:31,500  -->  00:02:32,640
.

37

00:02:32,640  -->  00:02:38,910
One reason why you want to pay for using the word final as opposed to the second approach that prevents

38

00:02:38,910  -->  00:02:45,780
inheritance is because it is instantiation which helps in defining state as well as defining instance

39

00:02:45,780  -->  00:02:48,250
methods which can work on that state.

40

00:02:48,510  -->  00:02:53,400
And that's what is happening with the string class where it is initialized with some text and then there

41

00:02:53,400  -->  00:02:58,850
are instance methods that can be used to manipulate that text.

42

00:02:58,860  -->  00:03:05,430
Now if you do not want your class to be extendable under at the same time to be not intangible then

43

00:03:05,430  -->  00:03:10,170
you would have to go with the second approach which is to use or NEAP private constructor.

44

00:03:10,560  -->  00:03:17,130
If you recall we discussed this while covering static maggot's he specifically discussed an effective

45

00:03:17,300  -->  00:03:24,210
diadem and said that if the class is a utility class like the map class where all the methods are static

46

00:03:24,600  -->  00:03:29,670
then you can make the class non-intentional by marking its concept private.

47

00:03:30,210  -->  00:03:35,880
We said that the benefit of doing it was that it would save us heaps because as we can no longer instantiate

48

00:03:35,880  -->  00:03:37,050
the class.

49

00:03:37,590  -->  00:03:43,370
And in fact it is meaningless to create such an instance when all its methods are static but we don't

50

00:03:43,370  -->  00:03:48,050
know that the second advantage is that the class is not extendable.

51

00:03:48,090  -->  00:03:52,060
There is also a technical reason on why it cannot be extended.

52

00:03:52,320  -->  00:04:00,120
And can you guess the reason and the reason is constipate is more pirate that is a private access where

53

00:04:00,210  -->  00:04:06,360
meets the constructor private to the class and so cannot be invoked from supperless.

54

00:04:06,610  -->  00:04:11,230
And if it cannot be in principle it means constructor chaining is not possible.

55

00:04:11,730  -->  00:04:15,110
So the subclass cannot be created.

56

00:04:16,320  -->  00:04:22,260
So to summarize if your class should not be extended but have stayed involved with methods depending

57

00:04:22,260  -->  00:04:28,440
on it then go for the final keyword example in string class as methods in the string class work on the

58

00:04:28,440  -->  00:04:30,870
strings input data.

59

00:04:30,870  -->  00:04:36,120
But if you are dealing with something like a utility class which includes only static methods then you

60

00:04:36,120  -->  00:04:43,140
can make the constructors blame it on it would present instantiation as an add on the class is no longer

61

00:04:43,230  -->  00:04:44,290
extendable.

62

00:04:44,790  -->  00:04:50,160
So if you want a utility class that only static methods that should not be extended then you can go

63

00:04:50,160  -->  00:04:52,720
for this private constructor option.

64

00:04:53,160  -->  00:04:57,960
But if you want your utility class to be extended then of course do not include the private constructor

65

00:04:58,460  -->  00:05:05,240
unload the class include only static markets and with that subclasses can add new methods.

66

00:05:05,310  -->  00:05:09,990
So that's a bug why and how you can prevent inheritance.

67

00:05:09,990  -->  00:05:15,040
Typically most inheritance hierarchies are wide but not deep.

68

00:05:15,090  -->  00:05:21,040
Usually they are one or two levels deep but there is no hard limit on it and just go ahead.

69

00:05:21,090  -->  00:05:27,480
On Monday user classes final on see how we get a compilation error on the staff plus as it extends the

70

00:05:27,480  -->  00:05:28,880
user class.

71

00:05:28,890  -->  00:05:29,280
Thank you
