Java Inner Classes

Introduction

Inner class means a class is declared inside another class that type of class is called inner classes.

Inner classes introduced in 1.1 version as part of “EventHandling” to resolve GUI bugs.

Inner classes are very powerful features and also so many benefits that’s why nowadays programmers start using regular coding.

 

 

Without existing one type of object, if there is no chance of existing another type of object then we should go for inner classes.

Example:

Without the existing College object, there is no chance of an existing Department object hence, we have to define the Department class inside the College class.

Note: The relationship between outer class and inner class is a Has-A relationship.

 

Syntax of Inner class

class Outer_class{ 
//......... 
class Inner_class{ 
//....... 
} 
}

Types of Inner classes :

Inner classes are divided into 4 types.

  • Normal (or) Regular inner class
  • Method Local inner classes
  • Anonymous inner classes
  • Static nested classes.

 

Normal (or) Regular inner class :

If we are declaring any named class inside another class directly without a static modifier such types of inner classes are called normal or regular inner classes. An inner class can access any private instance variable of the outer class.

 

Syntax of Inner class:

class Outer
{
//………
class Inner
{
//…….
}
}

 

Example:

package com.technicalround.demo
class Outer {
class Inner {
}
public static void main(String[] args) {
System.out.println("Outer class main method are calling");
}
}

 

We can’t declare static members inside the inner class. Hence it is not possible to declare the main() method and we can’t invoke inner class directly from the command prompt. If we are doing this then getting compile-time error.

Example:

package com.technicalround.demo
class Outer {
class Inner {
public static void main(String[] args) {
System.out.println("Inner class main method is calling");
}
}
}

Output:

Outer.java:5: inner classes cannot have static declarations
public static void main(String[] args)

 

What are modifiers support for outer classes:

public
default
final
abstracts
strictfp

What are modifiers support for outer classes:

private
protected
final
abstracts
strictfp

 

Nesting of Inner classes :

If we are declaring an inner class inside another inner class is called a nested inner class.

Example:

package com.technicalround.demo;
class A {
class B {
class C {
public void m1() {
System.out.println("C class method");
}
}
}
public static void main(String[] args) {
A a = new A();
A.B b = a.newB();
A.B.C c = b.newC();
c.m1();
}
}

 

Method local inner classes:

When we are declaring a class inside a method of an outer class such type of inner classes are called method local inner classes. Method Local inner classes are best suitable to meet the nested method requirements.

We can access the method’s local inner class only within the method where we declared it. That is from outside of the method we can’t access.

 

Example:

package com.technicalround.demo; 
class Demo {
public void methodOne() {
class Inner {
public void add(int i, int j) {
System.out.println("Total:" + (i + j));
}
}
Inner i = new Inner();
i.add(10, 20);
i.add(100, 200);
i.add(1000, 2000);
}
public static void main(String[] args) {
new Demo().methodOne();
}
}

Output:

Total: 30
Total: 300
Total: 3000

 

Anonymous inner classes:

When we declare inner class without any name such type of inner classes is called anonymous inner classes.

The main objective of anonymous inner classes is “just for instant use”.

 

Types of anonymous inner classes:

  • Anonymous inner classes are 3 Types.
  • Anonymous inner class that extends a class.
  • Anonymous inner class that implements an interface.
  • Anonymous inner class that defined inside method arguments.

 

Anonymous inner class that extends a class.

Example:

package com.technicalround.demo;
class PopCorn {
public void taste() {
System.out.println("Hot");
}
}
class Test {
public static void main(String[] args) {
PopCorn p = new PopCorn() {
public void taste() {
methodOne();// valid call(internal purpose)
System.out.println("Cool");
}
public void methodOne() {
System.out.println("child specific method");
}
};
//p.methodOne();//here we can not call(outside inner class)
p.taste();// Cool
PopCorn p1 = new PopCorn();
p1.taste();// Hot
}
}

 

Output:

Child-specific method
Salty
Spicy

 

Anonymous inner class that implements an interface.

Example:

package com.technicalround.demo;
class InnerClassesDemo {
public static void main(String[] args) {
Runnable r = new Runnable() // here we are not creating for Runnable interface,
// we are creating implements class object.
{
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Child thread");
}
}
};
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Main thread");
}
}
}

Anonymous Inner Class that defines inside method arguments:

Example:

package com.technicalround.demo;
class Test {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("child thread");
}
}
}).start();
for (int i = 0; i < 10; i++) {
System.out.println("main thread");
}
}
}

 

Static nested classes:

When we declare inner classes with static modifiers such types of inner classes are called static nested classes.

In the case of normal or regular inner classes without an existing outer class object, there is no chance of existing inner class objects. i.e., the inner class object is always strongly associated with an outer class object.

Example:

package com.technicalround.demo;
class Demo {
static class Nested {
public void methodOne() {
System.out.println("nested class method");
}
}
public static void main(String[] args) {
Demo.Nested obj = new Demo.Nested();
obj.methodOne();
}
}

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

 

Leave a Reply

Your email address will not be published.