World's most popular travel blog for travel bloggers.

Java Member inner class

A non-static class that is created inside a class but outside a method is called member inner class.
Syntax:
  1. class Outer{  
  2.  //code  
  3.  class Inner{  
  4.   //code  
  5.  }  
  6. }  

Java Member inner class example

In this example, we are creating msg() method in member inner class that is accessing the private data member of outer class.
  1. class TestMemberOuter1{  
  2.  private int data=30;  
  3.  class Inner{  
  4.   void msg(){System.out.println("data is "+data);}  
  5.  }  
  6.  public static void main(String args[]){  
  7.   TestMemberOuter1 obj=new TestMemberOuter1();  
  8.   TestMemberOuter1.Inner in=obj.new Inner();  
  9.   in.msg();  
  10.  }  
  11. }  
Output:
data is 30

Internal working of Java member inner class

The java compiler creates two class files in case of inner class. The class file name of inner class is "Outer$Inner". If you want to instantiate inner class, you must have to create the instance of outer class. In such case, instance of inner class is created inside the instance of outer class.

Internal code generated by the compiler

The java compiler creates a class file named Outer$Inner in this case. The Member inner class have the reference of Outer class that is why it can access all the data members of Outer class including private.
  1. import java.io.PrintStream;  
  2. class Outer$Inner  
  3. {  
  4.     final Outer this$0;  
  5.     Outer$Inner()  
  6.     {   super();  
  7.         this$0 = Outer.this;  
  8.     }  
  9.     void msg()  
  10.     {  
  11.         System.out.println((new StringBuilder()).append("data is ")  
  12.                     .append(Outer.access$000(Outer.this)).toString());  
  13.     }  
  14. }  

Nested Interface in Java


We can declare interfaces as member of a class or another interface. Such an interface is called as member interface or nested interface.
Interface in a class

Interfaces (or classes) can have only public and default access specifiers when declared outside any other class (Refer this for details). This interface declared in a class can either be default, public, private, protected. While implementing the interface, we mention the interface as c_name.i_name where c_name is the name of the class in which it is nested and i_name is the name of the interface itself.
Let us have a look at the following code:-
// Java program to demonstrate working of
// interface inside a class.
import java.util.*;
class Test
{
    interface Yes
    {
        void show();
    }
}
class Testing implements Test.Yes
{
    public void show()
    {
        System.out.println("show method of interface");
    }
}
class A
{
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj=t;
        obj.show();
    }
}
show method of interface 
The access specifier in above example is default. We can assign public, protected or private also. Below is an example of protected. In this particular example, if we change access specifier to private, we get compiler error because a derived class tries to access it.
// Java program to demonstrate protected
// specifier for nested interface.
import java.util.*;
class Test
{
    protected interface Yes
    {
        void show();
    }
}
class Testing implements Test.Yes
{
    public void show()
    {
        System.out.println("show method of interface");
    }
}
class A
{
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj=t;
        obj.show();
    }
}
show method of interface 

Interface in another Interface

An interface can be declared inside another interface also. We mention the interface as i_name1.i_name2where i_name1 is the name of the interface in which it is nested and i_name2 is the name of the interface to be implemented.
// Java program to demonstrate working of
// interface inside another interface.
import java.util.*;
interface Test
{
   interface Yes
   {
      void show(); 
   }
}
class Testing implements Test.Yes
{
   public void show()
   {
      System.out.println("show method of interface");
   }
}
class A
{
   public static void main(String[] args)
   {
     Test.Yes obj;
     Testing t = new Testing();
     obj = t;
     obj.show();
   }
}
show method of interface 
Note: In the above example, access specifier is public even if we have not written public. If we try to change access specifier of interface to anything other than public, we get compiler error. Remember, interface members can only be public..
// Java program to demonstrate an interface cannot
// have non-public member interface.
import java.util.*;
interface Test
{
    protected interface Yes
    {
        void show();
    }
}
class Testing implements Test.Yes
{
    public void show()
    {
        System.out.println("show method of interface");
    }
}
class A
{
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj = t;
        obj.show();
    }
}
illegal combination of modifiers: public and protected
   protected interface Yes