Inner Class
Inner Class
Inner Class
Nested Classes
In Java, just like methods, variables of a class too can have another class as its member. Writing a
class within another is allowed in Java. The class written within is called the nested class, and the
class that holds the inner class is called the outer class.
Syntax
The syntax to write a nested class is given below. Here the class Outer_Demo is the outer class
and the class Inner_Demo is the nested class.
class Outer_Demo{
class Nested_Demo{
}
}
Inner classes are of three types depending on how and where you define them. They are:
Inner Class
Method-local Inner Classlass
Anonymous Inner Class
Inner Class
Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class,
an inner class can be private and once you declare an inner class private, it cannot be accessed
from an object outside the class.
Given below is the program to create an inner class and access it. In the given example, we make
the inner class private and access the class through a method.
class Outer_Demo{
int num;
//inner class
private class Inner_Demo{{
public void print(){
System.out.println("This is an inner class");
}
}
//Accessing he inner class from the method within
void display_Inner(){
Inner_Demo inner = new Inner_Demo();
inner.print();
}
}
Here you can observe that Outer_Demo is the outer class, Inner_Demo is the inner class,
display_Inner is the method inside which we are instantiating the inner class, and this method is
invoked from the main method.
If you compile and execute the above program, you will get the following result.
To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using the
object of the outer class, you can instantiate the inner class as shown below.
The following program shows how to access the private members of a class using inner class.
class Outer_Demo {
//private variable of the outer class
private int num= 175;
//inner class
public class Inner_Demo{
public int getNum(){
System.out.println("This is the getnum method of the inner class");
return num;
}
}
}
If you compile and execute the above program, you will get the following result.
A method-local inner class can be instantiated only within the method where the inner class is
defined. The following program shows how to use a method-local inner class.
If you compile and execute the above program, you will get the following result.
If you compile and execute the above program, you will get the following result.
In the same way, you can override the methods of the concrete class as well as the interface using
an anonymous inner class.
But in all the three cases, you can pass an anonymous inner class to the method. Here is the
syntax of passing an anonymous inner class as a method argument:
obj.my_Method(new My_Class(){
public void Do(){
.....
.....
}
});
The following program shows how to pass an anonymous inner class as a method argument.
//interface
interface Message{
String greet();
}
class MyOuter {
static class Nested_Demo{
}
}
Instantiating a static nested class is a bit different from instantiating an inner class. The following
program shows how to use a static nested class.
If you compile and execute the above program, you will get the following result.