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



Download 7,31 Mb.
Pdf ko'rish
bet25/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   21   22   23   24   25   26   27   28   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

effectively final variable .
Anonymous inner class :
An inner class that does not have any name is called Anonymous
Inner class. You should use them when you want to use local class
only once. It does not have a constructor since there is no class
name and it cannot be declared as static.
Generally, they are used when you need to override the method of a
class or an interface.
When to use :
Example 1 : Let’s understand this by an example, Suppose you are
want to return a list of employee class objects and they should be
sorted based on employee name, now for this you can write a
comparator in a separate class and pass its object inside the
Collections.sort(list, comparatorObject)
Instead, you can use the anonymous inner class and you don’t have
to create a new class just for writing a comparator that you are not
going to use later on.
Program:
Employee.java :


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 1\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 1\color2.png


AnonymousInnerDemo.java :


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 1\color3.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 1\output.png


Example 2 : Using anonymous inner class, you can implement a
Runnable also


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 2\color.png


Output:
C:\Users\jj
atin\Desktop
\Different
Versions\All_
Photos\Ques
tion
47\anonymo
i
Example 3 : You can use anonymous inner class in situations where
you want to override the parent class method without creating a
separate child class:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 3\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 3\color2.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\anonymous inner class\Example 3\output.png


Some points to remember:

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   21   22   23   24   25   26   27   28   ...   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