Inner Class and Nested Interface in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Inner class and nested interface in java

Java inner class or nested class is a class that is declared inside the class or interface.

We use inner classes to logically group classes and interfaces in one place to be more readable and
maintainable.

Additionally, it can access all the members of the outer class, including private data members and
methods.

Syntax of Inner class

class Java_Outer_class{  
 //code  
 class Java_Inner_class{  
  //code  
 }  
}  

Advantage of Java inner classes

There are three advantages of inner classes in Java. They are as follows:

1. Nested classes represent a particular type of relationship that is it can access all the
members (data members and methods) of the outer class, including private.

2. Nested classes are used to develop more readable and maintainable code because it
logically group classes and interfaces in one place only.

3. Code Optimization: It requires less code to write.

Need of Java Inner class

Sometimes users need to program a class in such a way so that no other class can access it. Therefore,
it would be better if you include it within other classes.

If all the class objects are a part of the outer object then it is easier to nest that class inside the outer
class. That way all the outer class can access all the objects of the inner class. Difference between
nested class and inner class in Java

An inner class is a part of a nested class. Non-static nested classes are known as inner classes.
Types of Nested classes

There are two types of nested classes non-static and static nested classes. The non-static nested
classes are also known as inner classes.

o Non-static nested class (inner class)

1. Member inner class

2. Anonymous inner class

3. Local inner class

o Static nested class

Type Description

Member Inner Class A class created within class and outside method.

Anonymous Inner Class A class created for implementing an interface or extending class. The java compile

Local Inner Class A class was created within the method.

Static Nested Class A static class was created within the class.

Java Member Inner class

A non-static class that is created inside a class but outside a method is called  member inner class. It
is also known as a regular inner class. It can be declared with access modifiers like public, default,
private, and protected.

Syntax:

class Outer{  
 //code  
 class Inner{  
  //code  
 }  
}  

Java Anonymous inner class

Java anonymous inner class is an inner class without a name and for which only a single object is
created. An anonymous inner class can be useful when making an instance of an object with certain
"extras" such as overloading methods of a class or interface, without having to actually subclass a
class.
In simple words, a class that has no name is known as an anonymous inner class in Java. It should be
used if you have to override a method of class or interface. Java Anonymous inner class can be
created in two ways:

1. Class (may be abstract or concrete).

2. Interface

Java Local inner class

A class i.e., created inside a method, is called local inner class in java. Local Inner Classes are the inner
classes that are defined inside a block. Generally, this block is a method body. Sometimes this block
can be a for loop, or an if clause. Local Inner classes are not a member of any enclosing classes. They
belong to the block they are defined within, due to which local inner classes cannot have any access
modifiers associated with them. However, they can be marked as final or abstract. These classes have
access to the fields of the class enclosing it.

If you want to invoke the methods of the local inner class, you must instantiate this class inside the
method.

Java static nested class

A static class is a class that is created inside a class, is called a static nested class in Java. It cannot
access non-static data members and methods. It can be accessed by outer class name.

o It can access static data members of the outer class, including private.

o The static nested class cannot access non-static (instance) data members or

Java Nested Interface

An interface, i.e., declared within another interface or class, is known as a nested interface. The nested
interfaces are used to group related interfaces so that they can be easy to maintain. The nested
interface must be referred to by the outer interface or class. It can't be accessed directly.

Points to remember for nested interfaces

There are given some points that should be remembered by the java programmer.

o The nested interface must be public if it is declared inside the interface, but it can have any
access modifier if declared within the class.

o Nested interfaces are declared static

References:

https://www.javatpoint.com/java-inner-class

https://www.geeksforgeeks.org/interface-nested-class-another-interface
https://www.programiz.com/java-programming/nested-inner-class

You might also like