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