articles

Home / DeveloperSection / Articles / Java Inner Class

Java Inner Class

Anupam Mishra4337 11-Dec-2015

In java ,Inner class or nested class is declared inside the class or interface. We use inner class because it increases more readable & maintainable of code.

It can provide to access all the member of outer class including private data member or methods.

Class OuterEx
{
Class Inner {}
}
Advantage:
1.       It can access all the data members & methods of outer class including private.

2.       Increases readability & maintainability of code.

3.       Required less coding. 


Non-Static Nested Classes are also known as Inner Classes. 
I.       Non-Static Nested Class (Inner Class) 
    i. Member inner class
 A class i.e. declared outside a method and inside a class is called member inner
class.
class MemberOuter{  
 private int data=130;  
 class Inner{  
  void msg(){System.out.println("data : "+data);} 
 }  
 public static void main(String args[]){  
  MemberOuter ob=new MemberOuter(); 
  MemberOuter.Inner in=ob.new Inner();  
  in.msg(); 
 }  
 

Output:

130  

ii.      Anonymous class:
A class that have no name is known as anonymous inner class. It should be used if
you have to override method of class or interface. Anonymous inner class can be
created by two ways:

1.    Class (may be abstract or concrete).

2.    Interface

abstract class Emp{ 
  abstract void see();
}
class AnonymousInner{
 public static void main(String args[]){
  Emp p=new Emp(){
  void see(){System.out.println("Nice to
                      see you again");}
  };
  p.see();
 }
}  
Output:
Nice to see you again  

 iii.  Local inner class:

  A class i.e. created inside a method is known as a local inner class. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

Local variable can’t be private, public or protected. Local inner class cannot invoked from outside the method. 

class localInner{  
  void display(){  
  int value=500;//local variable must be final till jdk 1.7 only  
  class Local{  
   void msg(){System.out.println(value);}  
  }  
  Local l=new Local();  
  l.msg();  
 }  
 public static void main(String args[]){  
  localInner obj=new localInner ();  
  obj.display();  
 }  
}  
 

Output:

500  


II.      Static class

A static class i.e. created inside a class is called static nested class. It can’t access non-static data members and methods. It can be accessed by outer class name.

  • It can access static data members of outer class including private.
  • Static nested class can’t access non-static (instance) data member or method.


class OuterOne{ 
  static Sting
Name=”Anupam Mishra”
Name=”Anupam Mishra”;
  static class Inner{
   void msg(){System.out.println("Name
         is "+Name);}
  }
  public static void main(String args[]){
  OuterOne.Inner obj=new OuterOne.Inner();
  obj.msg();
  }
}  

Output:
Anupam Mishra 


III.     Nested Interface  

An interface i.e. declared within another interface or class is called a nested interface. The nested interfaces are used to group related interfaces so it can be easy to maintain. The nested interface must be referred by the outer interface or class. It can't be accessed directly class instance. 

interface Showing{ 
  void show();
  interface Message{
   void ss();
  }
}

class NestedInterface1 implements Showing.Message{
 public void ss(){System.out.println("This is nested interface");}

 public static void main(String args[]){
  Showable.Message message=new NestedInterface1();//interface
is upcasting here
  message.ss();
 }
}  

 

Output:

This is nested interface


Updated 27-Mar-2018

Leave Comment

Comments

Liked By