A presentation on the useful feature of Java of providing Nested Classes. A through description of the types of nested classes and the various ways to implement them is stated here.
2. Kinds of Classes in Java
• Top level or Outer classes
Declared inside package
Visible throughout package, perhaps further
Normally, although not always, declared in their own file
• public classes must be defined in their own file
• Nested and inner classes
Declared inside class (or method)
Can be visible only to outer class, or have wider visibility
3. Nested Classes
Writing a class within another class is allowed in
Java. The class written within is called the nested
class, and the class that holds the inner class is
called the outer class.
Syntax :
Here Outer_Demo is the outer class and Nested_Demo is the
nested class.
4. Types of Nested Classes
• Non-static nested classes :
These are the non-static classes also known as Inner
classes, which are not declared with the static keyword.
They can be further divided into –
Instance Inner classes
Method Local Inner classes
Anonymous Inner classes
• Static nested classes :
These are the static classes declared with the static
classes.
6. Non-static Nested (Inner) Class
• It is the non-static nested class defined in the scope of
another class or interface.
• Can directly access all variables and methods of
enclosing class (including private fields and methods)
and is associated with an instance of its enclosing
class.
• They are of three types and can be divided into –
Instance Inner classes
Method Local Inner classes
Anonymous Inner classes
7. Instance Inner Class
• It is the most basic type of non-static nested class.
• It is created when a class is written inside another
class and the inner class can be used to access the
private members of the outer class.
• To instantiate the inner class, the outer class has to
be instantiated first. Then, using the object of the
outer class, the inner class can be instantiated and its
methods can be called.
9. Method Local Inner Class
• It is the type of non-static nested class that is defined
in a block, typically in a method.
• Local inner class cannot be invoked from outside the
method.
• It can only access only the ‘final’ parameters of the
enclosing block, as it captures that variable or
parameter.
• Cannot have static data members (unless they are
declared final) and static methods.
11. Anonymous Inner Class
• Local classes with no name are called Anonymous classes.
• It helps to make code more concise by allowing to declare
and instantiate a class at the same time.
• It is used when a local class is to be used only once.
• It is often used to override method of a class or interface.
// Anonymous inner class for event handling
12. Accessing the private members of
an Inner Class
• To instantiate the inner class, the outer class has to be
instantiated first. Then, using the object of the outer class,
the inner class can be instantiated and its private members
can be accessed.
13. Accessing the private members of
an Inner Class (continued …)
• On compiling and executing the code, the result is –
• The ‘getNum()’ method of the inner class is called
from the other class after instantiating it with the
outer class.
• Syntax :
14. Static Nested Classes
• A static nested class is a nested class which is a
static member of the outer class. It can be
accessed without instantiating the outer class,
using other static members.
• Just like static members, a static nested class
does not have access to the instance variables
and methods of the outer class.
• Syntax :
15. Instantiating Static Nested Classes
• A static nested class is instantiated in a bit
different way from non – static inner classes.
• Syntax :
16. Instantiating Static Nested Classes
(continued …)
• On compiling and executing the code, the result
is –
• The ‘my_method()’ method of the nested class is
called from the other class after instantiating it
without instantiating the outer class.
• Syntax :
17. Advantages of Nested Classes
• It is a way of logically grouping classes that are only used in one place
• Can access all the members of the outer class including private data
members and methods.
• Can be declared private, public, protected or package private, unlike
regular classes that can be only declared public or package private.
• It increases encapsulation as itself can be declared private and still
access the outer class’ private members
• It can lead to more readable and maintainable code as it places the
code closer to where it is used.