Categories: Java

Inner class in Java

In Java, an inner class is a class that is declared within another class or interface. To summarize, the same logically related classes as Java is purely object-oriented, bringing it closer to the real world.
It also has access to all the outer class’s members, including private data members and methods.

Syntax:
class Outer_Demo {
    //statements...
   class Inner_Demo {
      //statements...
   }
}
Why do we use inner classes?

The following are some advantages associated with the inner classes:

  • Making code that is clean and readable.
  • Private methods of the upper class can be accessed, adding a new dimension and bringing it closer to reality.
  • The code module is being optimized.
Types of Inner classes:
  • Non-static nested class (inner class)
    1. Member inner class
    2. Anonymous inner class
    3. Local inner class
  • Static nested class
Examples:
Nested Inner Class :

class Outer {
    
 // Simple nested inner class
 class Inner {

  // show() method of inner class
  public void show()
  {

   // Print statement
  
   for(int i=0;i<5;i++)
   {
        for(int j=0;j<i;j++)
   {
        System.out.print(j+"");
   }
        System.out.println();
   }
    System.out.println("In a nested class method");
   
  }
 }
}

// Class 2
// Main class
class Coderz {

 // Main driver method
 public static void main(String[] args)
 {

  Outer.Inner in = new Outer().new Inner();

  // Calling show() method over above object created
  in.show();
 }
}
Output:
0
01
012
0123
In a nested class method
Local Inner Classes :

class Outer {

 // Method inside outer class
 void outerMethod()
 {

  // Print statement
  System.out.println("inside outerMethod");

 
  class Inner {

   // Method defined inside inner class
   void innerMethod()
   {

   
    System.out.println("inside innerMethod");
   }
  }

  // Creating object of inner class
  Inner y = new Inner();

  // Calling over method defined inside it
  y.innerMethod();
 }
}


class Coderz {

 // Main driver method
 public static void main(String[] args)
 {

 
  Outer x = new Outer();

 
  x.outerMethod();
 }
}
Output:
inside outerMethod
inside innerMethod
Static Nested Classes:

import java.util.*;

class Outer {

 private static void outerMethod()
 {

  // Print statement
  System.out.println("1. inside outerMethod");
 }


 // Static inner class
 static class Inner {

  public static void display()
  {

   // Print statement
   System.out.println("2. inside inner class Method");

   // Calling method inside main() method
   outerMethod();
  }
 }
}


// Main class
class Coderz {

 // Main driver method
 public static void main(String args[])
 {

  Outer.Inner obj = new Outer.Inner();

  // Calling method via above instance created
  obj.display();
 }
}
Output:
2. inside inner class Method
1. inside outerMethod
Anonymous Inner Class:

import java.util.*;


class Demo {

 
 void show()
 {
  // Print statement
  System.out.println(
   "1. show method of super class");
 }
}

// Class 2
// Main class
class Flavor1Demo {

 // An anonymous class with Demo as base class
 static Demo d = new Demo() {
 
  void show()
  {
   // Calling method show() via super keyword
   // which refers to parent class
   super.show();

   // Print statement
   System.out.println("2. Flavor1Demo class");
  }
 };

 // Method 2
 // Main driver method
 public static void main(String[] args)
 {
  // Calling show() method inside main() method
  d.show();
 }
}
Output:
1. show method of super class
2. Flavor1Demo class

Note: also read about the Java connectivity to MongoDB database

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago