Inner Classes in Java (Nested Classes)

 It is also possible to nest classes in Java (a class within a class). Nested classes are being used to combine classes that belong together, making your code easier to read and maintain.

  • Making code that is readable and easy to understand.
  • The outer class's private methods can be accessed, giving it a new dimension and bringing it closer to reality.
  • The code module is being optimized.
Inner Classes in Java
java inner classes


Inner classes are divided into two types :

Non-static nested classes: 

These are a class's non-static members.
  • Inner Classes
  • Method Local Inner Classes
  • Anonymous Inner Classes

Static nested classes :

These are the static members of a class.

Hierarchy of Inner classes :

Hierarchy of inner classes
Hierarchy of inner classes

Inner Class :

The process of creating an inner class is simple. Just you have to do is create a class within a class. unlike a class, a inner class,  can be private, and if it is declared private, it cannot be accessible from outside the class.
Example:
class OuterClass {
int num;

// inner class
private class InnerClass {
public void print() {
System.out.println("This is an inner class");
}
}

// Accessing he inner class from the method within
void display_Inner() {
InnerClass obj = new InnerClass();
obj.print();
}
}

public class MyMainClass {

public static void main(String args[]) {
// Instantiating the outer class
OuterClass obj1 = new OuterClass();

// Accessing the display_Inner() method.
obj1.display_Inner();
}
}

Output :
This is an inner class


Accessing Private Members :

Inner classes are also used to gain access to a class's private members. Suppose a class has private members who can only be accessed by the class's members. Create an inner class in it, using a method within the inner class to return the private members.


Example:
class OuterClass {
// private variable of the outer class
private int num = 156;

// inner class
public class InnerClass {
public int getMember() {
System.out.println("This is the getMember method of the inner class");
return num;
}
}
}

public class MyMainClass {

public static void main(String args[]) {
// Instantiating the outer class
OuterClass outer = new OuterClass();

// Instantiating the inner class
OuterClass.InnerClass inner = outer.new InnerClass();
System.out.println(inner.getMember());
}
}

Output :
This is the getMember method of the inner class: 156


Method Local Inner Class :

An inner class can be declared within an outer class's method. Like local variables, the scope of the inner class is restricted within the method. Only the method where the inner class is declared can be used to instantiate a method-local inner class.

Example:

public class Outerclass {
// instance method of the outer class
void OuterClsMethod() {
int num = 125;

// method-local inner class
class MethodInnerClass {
public void show() {
System.
out.println("This is method inner class " + num);
}
}
// end of inner class

// Accessing the inner class
MethodInnerClass inner =
new MethodInnerClass();
inner.
show();
}

public static void main(String args[]) {
Outerclass outer =
new Outerclass();
outer.
OuterClsMethod();
}
}

Output :
This is method inner
class 125



Anonymous Inner Class :

An anonymous inner class is one which is defined without a class name. We declare and instantiate anonymous inner classes at the same time in this example. They're often used when you need to override a class's or interface's method.

Example :

abstract class Test 
{
abstract void eat();
}

class TestAnonymousInner
{
public static void main(String args[])
{
Test t = new Test()
{
void show()
{
System.out.println("hello");
}
};
t.show();
}
}

Output :
hello


Static Nested Class :

A nested class which is a static member of the outer class is called a static inner class. Other static members can be used to access it without having to instantiate the outer class. A static nested class, like static members, has no access to the outer class's instance variables and methods.

Example :

public class Outer 
{
static class NestedClass
{
public void TestMethod()
{
System.out.println("This is nested class.");
}
}

public static void main(String args[])
{
Outer.NestedClass nested = new Outer.NestedClass();
nested.TestMethod();
}
}

Output :
This is nested class.
.

Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post