Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview



Download 7,31 Mb.
Pdf ko'rish
bet32/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   28   29   30   31   32   33   34   35   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 54: What is Cloneable?
Answer: Cloneable is an interface in Java which needs to be
implemented by a class to allow its objects to be cloned.
A class implements the Cloneable interface to indicate to the
Object.clone() method that it is legal for that method to make a field-
for-field copy of instances of that class.
If you try to Clone an object which doesn’t implement the Cloneable
interface, it will throw CloneNotSupportedException.
Example without implementing Cloneable interface:
Program 1:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 1\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 1\color2.png


Output:
C:\Users\jjatin\Desktop\Different
Versions\All_Photos\Question 54\Program 1\output.png


Here, we have created Employee object e1 with new keyword but
then we created another object Employee e2 which has the same
reference as of e1. So, any change in e2 object will reflect in e1
object and vice-versa.
Now, let’s implement Cloneable interface in our Employee class and
invoke the clone() method on e1 object to make its clone:
Program 2:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 2\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 2\color2.png


Output:
C:\Users\jjatin\Desktop\Different
Versions\All_Photos\Question 54\Program 2\output.png


In the above example, we have only primitive types in our Employee
class, what if we have an object type i.e. another class object
reference, see the example below:
Program 3:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 3\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 3\color2.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 3\color3.png


Can you guess the output of the last 2 sysout? Here is the output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
54\Program 3\output.png


If you are surprised with the above output, then let me make it clear
by saying that, by default Object’s clone() method provide Shallow
copy. This brings us to the next interview question: What is shallow
copy and deep copy

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   28   29   30   31   32   33   34   35   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish