Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Inner Class
3.
Types of Inner Class
3.1.
Nested Inner Classes
3.1.1.
Code 1
3.2.
Method Local Inner Class
3.2.1.
Code 2
3.3.
Static Nested Classes
3.3.1.
Code 3
3.4.
Anonymous Inner Class
3.4.1.
Code 4a
3.4.2.
Code 4b
4.
Advantages of Inner Class
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to Inner Class

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In Java, just like the methods, the variables of a class can have another class as its member. So, in this blog, we will be explaining the introduction of the inner class.

Recommended Topic-  Iteration Statements in Java, Duck Number in Java

Inner Class

An inner class is a class declared inside a class or an interface. The existence of an inner class makes our code more readable and maintainable. The purpose of an inner class is to bind the group classes together that belong to the same category. To access this inner class, one must create an object of the outer class and then make an object of the inner class.

Syntax

class Java_Outer_class {
	//code
	class Java_Inner_class {
		//code 
	}
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Inner Class

  • Nested Inner Classes
  • Method Local Inner Classes
  • Static Nested Classes
  • Anonymous Inner Classes

Nested Inner Classes

This inner class helps to access any private instance of the outer class. We can access the private modifier, protected, public, and default modifier in this category. An interface can also be nested inside and have access specifiers like a class.

Syntax

Outer_Demo outer = new Outer_Demo();
Outer_Demo.Inner_Demo = inner = outer.new Inner_Demo();

Code 1

class Outer_Demo
{
  
	private class Inner_Demo 
	{
		public void print() 
		{
			System.out.println("This is an inner class.");
		}
	}
	void display_Inner() 
	{
		Inner_Demo inner = new Inner_Demo();
		inner.print();
	}
}
  
public class Nested_Inner
{
	public static void main(String args[]) 
	{
		Outer_Demo outer = new Outer_Demo();
		outer.display_Inner();
	}
}

Output

This is an inner class.

In this example, we have created two classes, one outer class, and one inner class, internal to the outer class. We have separately created two class objects called the outer and inner functions, declared inside the classes. 

Also read, Swap Function in Java

Method Local Inner Class

When a class has written within a method of this type, which will be local to this Method, this category of inner class is declared within a method of an outer class, and this can be accessed by creating a local object of an inner class. Like local variables, the scope of the inner class is limited within the Method.

Syntax

class Outer {
	//code
	Class Inner{
		Inner in = new Inner();
	}
}

Code 2

class Outer 
{ 
    void outerMethod()
    { 
        System.out.println("inside outer Method");
        
        class Inner
        {
            void innerMethod()
            {
                System.out.println("inside inner Method");
            }
        }
     
    Inner inn = new Inner();
     
    inn.innerMethod();
    }
}
    
public class Method_Local_Inner{
     
    public static void main(String[] args)
    {
        Outer out = new Outer();
        out.outerMethod();
    }
}

Output

inside outer Method
inside inner Method

In this example, we have used a method local inner class. We have simply created the object of the method local class in the same class and the outer class object in the main Method.

Static Nested Classes

A static inner class is a type of class that is a nested class and static member of the outer class. It can be accessed without creating the object of the outer class. It is like a static member, which does not have access to instance variables and methods of the outer class.

Syntax

class MyOuter {
	static Class Nested_Demmo {
	}
}

Code 3

class OuterDemo {
    private static void outerMethod()
    {
        System.out.println("inside outer Method");
    }
    static class Inner {
        public static void display()
        {
            System.out.println("inside inner class Method"); 
            outerMethod();
        }
    }
}
public class Static_Nested {
 
    // Main driver method
    public static void main(String args[])
    {
 
        OuterDemo.Inner temp = new OuterDemo.Inner();
		temp.display();
    }
}

Output

inside inner class Method
inside outer Method

In this example, we have created two classes and simply call the function defined in the outer class, and it will print whatever is in the function.

Also see, Java Ioexception

Anonymous Inner Class

When an inner class is declared without a name, it has termed the anonymous inner class. In this case of the anonymous inner class, we declare and instantiate them simultaneously. 

Syntax

AnonomousInner an_Inner = new AnonymousInner() {
	public void my_method() {
		//code
	}
};

Code 4a

interface Hello {
 
    void show();
}
class Anonymous_Inner{
 
    static Hello hl = new Hello()
     {            
        public void show()
        {           
            System.out.println("I am in the anonymous class.");
        }
    };  
    public static void main(String[] args)
    {
        hl.show();
    }
}

Output

I am in the anonymous class.

In this example, we have taken an anonymous class, we also have created the interface along with its object, then we simply called the function to print the further.

Code 4b

import java.util.*; 
class Demo {
void show() {
 System.out.println( "I am in show method of the superclass"); 
}
 } 
class Flavor1Demo { 
static Demo d = new Demo() {
 void show() {
 super.show(); 
System.out.println("I am in Flavor1Demo class"); 
}
 };
 public static void main(String[] args) { 
d.show();
 }
 }

Output

I am in show method of the superclass
I am in Flavor1Demo class

In this example, we have used a different implementation of an anonymous inner class. Here we have created two classes. Demo one acts as a superclass, and the anonymous class acts as a subclass. We have used show methods in both classes.

You can also read about the topic of Java Destructor and Hashcode Method in Java.

Advantages of Inner Class

  1. The inner class should be used by an outer class. An inner class represents the relationship that helps to access all data members and methods which includes the private class.
     
  2. The inner class helps us to develop more readable and representable code as they logically place classes and interfaces in a readable manner.
     
  3. It allows code optimization which requires us to write less code. 

Read More,  even number program in java

FAQs

  1. What are the advantages of using the inner class?
    This class represents a particular type of bond which has access to all its members of the outer class, which includes the private. These classes are used to develop more readable and maintainable code. It also optimizes the code.
     
  2. What are the types of nested classes?
    Nested classes can be explicitly divided into two types which are Non-static classes and Static nested classes. The non-static class can be further divided into three types: Method inner class, Anonymous class, and Local inner class.

Key Takeaways

In this blog, we covered the inner class. We briefly introduced what the inner class is, along with its syntax. We also explained the different types of inner classes and their syntaxes. We also discussed several examples of these types of inner classes.

If you want to explore more about these concepts in java, please visit our website and have a look at Data Structure Algorithms in Java course curated by experts.

Live masterclass