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

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago