Nested Classes

Nested and Inner classes
A class declared inside a class is known as nested class.
We use nested classes to logically group classes and interfaces in one place so that it can be more readable and maintainable code.
Additionally, it can access all the members of outer class including private data members and methods.
Advantage of nested classes
There are basically three advantages of nested classes. They are
* Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
* Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
* Code Optimization: It requires less code to write.
Types of Nested class:
Any class which is not a top level or declared inside another class is known as nested class and out of those nested classes, class which are declared non static are known as Inner class in Java. There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1 non-static nested class(inner class)
a)Member inner class
b)Annomynous inner class
c)Local inner class
2 static nested class
1 non-static nested class(inner class)
A non-static inner class can be instantiated only inside a non-static method of the outer class. This is because every instance of a non-static inner class must be associated with an instance of the outer class. In a sense, every instance of a non-static inner class exists ``inside'' an instance of the outer class. A single instance of the outer class may have associated with it more than one instance of the inner class.
Because an instance of a non-static inner class has an associated instance of the outer class, the methods of the inner class can access directly any of the members (fields or methods) of the outer class instance.
a) Member inner class: -
A class that is declared inside a class but outside a method is known as member inner class.
Invocation of Member Inner class
1 From within the class
2 From outside the class
1 Example of member inner class that is invoked inside a class
Example: - We are invoking the method of member inner class from the display method of Outer class.
class Outer{
private int data=30;
class Inner{
void msg(){System.out.println("data is "+data);}
}
void display(){
Inner in=new Inner();
in.msg();
}
public static void main(String args[]){
Outer obj=new Outer();
obj.display();
}
}
Output:data is 30
2 Example of member inner class that is invoked outside a class
Example: - We are invoking the msg() method of Inner class from outside the outer class i.e. Test class.
//Program of member inner class that is invoked outside a class
class Outer{
private int data=30;
class Inner{
void msg(){System.out.println("data is"+data);}
}
}
class Test{
public static void main(String args[]){
Outer obj=new Outer();
Outer.Inner in=obj.new Inner();
in.msg();
}
}
Output:data is 30
2 Annonymous inner class
A class that have no name is known as annomymous inner class.
Annonymous class can be created by:
1 Class (may be abstract class also).
2 Interface
Program of annonymous inner class by abstract class
abstract class Person{
abstract void eat();
}
class Emp{

public static void main(String args[]){
Person p=new Person(){
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
Output:nice fruits
Program of annonymous inner class by interface
interface Eatable{
void eat();
}
class Emp{
public static void main(String args[]){
Eatable e=new Eatable(){
public void eat(){System.out.println("nice fruits");}
};
e.eat();
}
}
Output:nice fruits
3 Local inner class
A class that is created inside a method is known as local inner class. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.
Program of local inner class
class Simple{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
Simple obj=new Simple();
obj.display();
}
}
Output:30
Rules for Local Inner class: -
1 Local inner class cannot be invoked from outside the method.
2 Local inner class cannot access non-final local variable.
3 Local variable can't be private, public or protected.
Program of accessing final local variable in local inner class
class Simple{
private int data=30;//instance variable
void display(){
final int value=50;//local variable must be final
class Local{
void msg(){System.out.println(data+" "+value);}//ok
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
Simple obj=new Simple();
obj.display();
}
}
Output:30 50
4 static nested class.
A static class that is created inside a class is known as static nested class. It cannot access the non-static members.
* It can access static data members of outer class including private.
* static nested class cannot access non-static (instance) data member or method. Program of static nested class that have instance method
class Outer{
static int data=30;
static class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
Outer.Inner obj=new Outer.Inner();
obj.msg();
}
}
Output:data is 30
Nested Interface
An interface which is declared within another interface or class is known as nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred by the outer interface or class. It can't be accessed directly.
Points to remember for nested interfaces
There are given some points that should be remembered by the java programmer: -.
1 Nested interface must be public if it is declared inside the interface but it can have any access modifier if declared within the class.
2 Nested interfaces are declared static implicitely.
Example : - nested interface which is declared within the interface
interface Showable{
void show();
interface Message{
void msg();
}
}
class Test implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new Test();//upcasting here
message.msg();
}
}
Output:hello nested interface
Example of nested interface which is declared within the class
class A{
interface Message{
void msg();
}
}
class Test implements A.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
A.Message message=new Test();//upcasting here
message.msg();
}
}
Output:hello nested interface


Free Web Hosting