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


Question 47: What is an Inner Class in Java, how it



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

Question 47: What is an Inner Class in Java, how it
can be instantiated and what are the types of Inner
Classes?
Answer: In Java, when you define one non-static class within
another class, it is called Inner Class/Nested Class. Inner class
enables you to logically group classes that are only used in one
place, thus, this increases the use of encapsulation and creates
more readable and maintainable code.
Java inner class is associated with the object of the class and they
can access all the variables and methods of the outer class. Since
inner classes are associated with the instance, we can’t have any
static variables in them.


The object of java inner class is part of the outer class object and to
create an instance of the inner class, we first need to create an
instance of outer class.
Java Inner classes can be instantiated like below:
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Example:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\Inner Class instantiation\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\Inner Class instantiation\color2.png


Output:
C:\Users\jjatin\
Desktop\Differe
nt
Versions\All_Ph
otos\Question
47\Inner Class
instantiation\out
put.png
Compile time error when static variable and static method is present
in Inner class:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\Compile time error when Inner Class have static fields\color.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
47\Compile time error when Inner Class have static fields\error.png


There are 2 special types of Inner Classes:

Download 7,31 Mb.

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