Object Oriented Programming Nested Classes: by Irfan Latif Memon
Object Oriented Programming Nested Classes: by Irfan Latif Memon
Nested Classes
Lecture 12
By
Irfan Latif Memon
Outline
Nested Classes
Inner Class within any block scope
Nested Classes or Inner Class
• Java inner class or nested class is a class which
is declared inside the class or interface.
• We use inner classes to logically group classes
and interfaces in one place so that it can be
more readable and maintainable.
• Additionally, it can access all the members of
outer class including private data members
and methods.
Two types of nested classes
• There are two types of nested classes:
• static and non-static.
• A static nested class is one that has the static
modifier applied. Because it is static, it must
access the members of its enclosing class through
an object.
• That is, it cannot refer to members of its
enclosing class directly. Because of this
restriction, static nested classes are seldom used.
Two types of nested classes
• The most important type of nested class is the
inner class. An inner class is a non-static
nested class.
• It has access to all of the variables and
methods of its outer class and may refer to
them directly in the same way that other non-
static members of the outer class do.
There are two types of nested classes: