Not let's be clear of you I don't want to be from Effective Java which says prefer class hierarchies 2
to tactlessness. 3
We know that this is a class hierarchy for present bookmarks. 4
It has four classes with one super class and three subprocess. 5
Just note that in addition Weiss the abstractness bookmark is specifically marked with the keyword abstract 6
right about the class. 7
OK an item says that we need to use such a class hierarchy instead of a bag class. 8
So let's look at what a that class as on why we shouldn't use it. 9
Here is an example of a stack class and with this design instead of four classes we know how only one 10
class which is the bookmark us so no separate classes to represent weblink movie and book bookmark itself 11
. 12
Below represent these entities and for the sake of brevity some of the information has been left out 13
only a few fees related to movie unbroke are included and that's sufficient to understand what it across 14
. 15
Basically the way it works is it inkers instance variables are relevant to both movie and book. 16
When a bookmark object is created and if it is off by book then only feels relevant to book or initialized 17
. 18
For example as much as a book then co-anchored would invoke the first constructor and pass the title 19
of the book as well as the authors of the book. 20
So only that relevant instance variables Baida and authors get initialized while the other movie related 21
variables like directors and cast are just ignored. 22
Also there is this field called bookmark type which will be initialized the w book. 23
So the more course information about the type of book mark and be referred to it as the backfield as 24
it is kind of tagging the bookmark object as either a book or a movie. 25
Nice bookmark is a movie then client code would invoke the second constructor and would pass the title 26
of the movie as well as the directors of the movie along with the cast permission. 27
Not only instance variables are relevant to movie or initialized Well author's field is left untouched 28
and marked by variable will now be initialized with the value Maulvi just know that. 29
In this class if multiple values are to be assigned to variables like auteurs detectors on cast then 30
those values would be concatenated in some way to form a string. 31
Example would be a comma operator list. 32
It is fine for this discussion but in practice you should avoid using such kind of concatenated values 33
. 34
You would instead use Arrius or other data structures like orderless. 35
Next if you look at ease keep friendly eligible motet you can notice that it includes separate logic 36
depending on the type of book 1. 37
So basically the item in effective Java says that we shouldn't design such a class due to several reasons 38
that are evident here in this illustration. 39
One reason is it is boss that is you have multiple constructors too many feels on too much of logic 40
and one single method. 41
Second reason is it is error prone as you are trying to represent too many things in the same class 42
. 43
Recall the classes should be designed with single responsibility principle which was discussed in coding 44
conventions. 45
Listen Finally it is inefficient. 46
That is English memory footprint as any object will include some irrelevant feats. 47
For example when creating a book object we need to unnecessarily look at space for the variables directors 48
and uncashed. 49
So that's not good. 50
So basically that class is not good and sometimes out of ignorance. 51
Engineers might write such process. 52
We don't know that we shouldn't do this. 53
Since Julys an object oriented language. 54
We should try to use it class hierarchy like the way we have done for the bookmark process. 55
Now in case you encounter that class you may want refactoring. 56
And let's see how that can be done. 57
Let's just refactor our Mark back to us. 58
Make the backglass an abstract class. 59
Also make every method whose behavior depends on the track field as an abstract method. 60
In our case we have only one such method. 61
If there are any methods that do not depend on the fact free let them remain as concrete methods in 62
our example. 63
We do not have such methods. 64
Similarly if there are any fields that are used by all types of that class then those fields should 65
be in this abstract class. 66
And in our example it would be the ideal variable as title applies to both movies as well as book. 67
Next we find a concrete subclass for each type of duck back to us. 68
The concrete classes should override the abstract methods in the superclass thus providing a specific 69
implementation. 70
Finally most any subclass specific variables from superclass include a corresponding subclass. 71
So the more authors to book class and drippers on cost to movie class with this design there are no 72
more irrelevant things and the code is simple and clear. 73
Another advantage is that the code can lead to better design and more flexibility. 74
For example let's say we want to create an ebook as a special type of book. 75
Then we can simply create a new class called ebook which would extend the bookless under present in 76
the same with a class could lead to more confusing or so as we know inheritance Palmet better design 77
and we are also not touching our own code and we are simply introducing this new class. 78
So that's the flexibility aspect. 79
So in summary you should prefer using a class hierarchy and in case you want to attack class C if you 80
want to refactor it into a class hierarchy. 81
Thank you