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


- Using Externalizable interface, we can implement custom logic for serialization and deserialization of object -



Download 7,31 Mb.
Pdf ko'rish
bet40/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   36   37   38   39   40   41   42   43   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Using Externalizable interface, we can implement
custom logic for serialization and deserialization of
object
When using Externalizable, we have to explicitly mention
what fields or variables we want to serialize
When using Externalizable, a public no-arg constructor
is required
Using Externalizable, we can also serialize transient and
static variables
readExternal() method must read the values in the same
sequence and with the same types as were written by
writeExternal() method
Question 61: Externalizable with Inheritance
If a class is implementing Externalizable and also has a parent class,
how we can save the data of parent class and how we can recover it
back. In case of Externalizable, we have to implement the
readExternal() and writeExternal() methods in each and every class
we want to serialize or deserialize.
Case 1 : When both parent and child classes are implementing
Externalizable interfaces:
Department.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color2.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color3.png


Student.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color4.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color5.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color6.png


Here, in the overridden writeExternal() and readExternal() methods
in child class Student, we are also calling super.writeExternal() and
super.readExternal() methods to serialize and de-serialize fields of
parent class Department.
ExternalizableDemo.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color7.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color8.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\color9.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 1\output.png


The capacity and age variable values are 0 because we did not
serialize these two variables.
Case 2 : When only child class is implementing the Externalizable
interface
Department.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color2.png


Student.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color3.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color4.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color5.png


Here, we are using getters and setters of parent class, Department,
to serialize and de-serialize its fields.
ExternalizableDemo.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color6.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color7.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\color8.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
61\Case 2\output.png



Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   36   37   38   39   40   41   42   43   ...   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