Next let's look at another widely used marker interface called Honorable from the Java dot land package 2
. 3
Even several classes in July that you package do implement Grown-Up an interface by implement inclinable 4
class declares that its objects can read and their clients. 5
That is there copies but copy we mean a duplicate object is written which would have the same state 6
as the original object. 7
But question is Where does this GRONING capability come from. 8
Simply implementing Markin interface cannot magically do this whether the clone method in Object class 9
is just the briefly mentioned does matter while discussing Object class. 10
And we said that we will discuss it later. 11
So let's look at it now. 12
But convention tuneable functionality is implemented in this way. 13
The class obviously should implement the marketing disclosable. 14
Then you need to provide a method that supports cloning. 15
Convention is to name the method as clone. 16
But you can do a different name. 17
Do you need to invoke the clone method in object to us since it's a superclass when we look use in the 18
supercooled the current method in Object class would actually do all the heavy lifting on it and a copy 19
of the object. 20
However the copy it again would be of type Object and hence we need to cast it before returning it back 21
to the caller. 22
The clone method in Object class would check whether or not the current object implements global interface 23
. 24
If it does it would return a copy of the object. 25
Otherwise it would throw an exception called Glue not supported exception. 26
The Cricut end of that you see here and closing the method in location is related to handling of that 27
exception. 28
But let's not worry about it for now as we have not yet discussed exceptions. 29
Note that if this method is not defined Bruening will just not work. 30
Even if you have implemented Coonamble in the declaration you can simply end were blown metor on the 31
object assuming it would directly involved the clone method in the object class that would not work 32
as grown in Object class has protected access and hence can be invoked from only a subclass. 33
So you need this kind of a method to support GRONING not that it's not necessary that you strictly have 34
to invoke object classes clone. 35
You're free to perform that cloning yourself. 36
Finally the clone method performs what is called a shallow copy in order to generate a duplicate object 37
shallow copy implies that when generating the new object defeating the origin an object or not Gloria 38
did. 39
Values are simply assigned to the corresponding fields in the new object had the fields been closed 40
. 41
Then we would call the process as deep copy. 42
One problem that shall look up is that if a field is referencing a mutable object then Buddha origin 43
an object as well as its clone would be referencing the same object. 44
So if one of them modifies the mutable object then the other will also see the change. 45
And that's not good. 46
But if the fields are all primitives of reference in immutable objects then copy is not a problem. 47
So if you know that there are any mutable objects. 48
Then what you can do is once you get the grant from the superclass you can explicitly Glenda's in denial 49
mutable objects and then read in the going back to the caller. 50
Finally know that experts like Joshua Knoblock consider that it's a design flaw to have to enable as 51
a marketing office. 52
It would have been a mixing interface. 53
As you can see the subclass is forced to implement a clone method in order to define the clone functionality 54
. 55
In that case why not have tuneable as a mixed in with gunas it's only meant that this is just because 56
an item level in effective Java altered due to code the supplementary not for some relevant an interesting 57
point on this topic. 58
McGoohan very quick demo of the clone method. 59
Here is a quote from the interview demo. 60
You may recall that we have this class called X which was X turning abstract 8 I was also implementing 61
three interfaces A B and C. 62
So for this day I have introduced this global interface so it's implemented inclinable. 63
And we also have this clone method which is exactly identical to what we have seen in this light. 64
And so the goal is to create an instance an object of X and then invoke this method which would then 65
return a clone off that object a duplicate of that object. 66
So that is done from this method. 67
This class desk. 68
So here we are instantiating X and then here we are in working the clone method here the reference is 69
C which is an interface. 70
And it does not include the clone method. 71
So that's the reason we have to downcast it we have to Buddhist typecast and then invoke the method 72
and then the clone and then we get a clone object a clone and obviously that clone and the actual instance 73
are two different instances. 74
So they are not equal. 75
And then this Gruinard should be brain. 76
So let me just go ahead and compile and run this. 77
So here it is. 78
It goes on princedom grown created message that we see here. 79
Now let me just go back to X. Let me comment this thing. 80
If we are commandeers we need to get a clone non-supported exception because that's the check that not 81
that that a clone method in the object class makes. 82
So here we are invoking the going into object and that would make a check. 83
And since Grown-Up it is not and is not implemented here we should get a trick or an exception. 84
So the American public as you can see the clone method here in the object class is throwing this exception 85
called clone non-supported exception. 86
So that's that's what is being presented here. 87
And that's about it.