Wednesday, 1 February 2017

INNER CLASS IN JAVA

Sometimes, we can declare classes inside the class, such type of classes are called inner classes. Inner class concept was introduced in java 1.1v. It supports AWT and string components event handling.

Need of an inner class – Inner class is used for creating an object logically inside another object with clear separation of properties region. Hence, we can know that a particular object belongs to which object.

Example –
public class Test1 {
          int empno;
          String ename;
          class Test2{
                   int hno;
                   String city;
                   String state;
          }
}

Explanation – If the Test2 properties are needed for more than one outer object, then we must create Test2 class as outer class, and we should create this class object in that outer object class with HAS-A relation.


Based on the purpose and position of declaration, all the inner classes are divided into following four types –

1. Normal or Regular inner class – If we declare any named class inside a class directly without the static modifier, such type of inner classes are called Normal or Regular inner classes.

Example –
public class Test1 {
          int empno;
          String ename;
          class Test2{
                   int hno=10;
                   String city;
                   String state;
          }
          public static void main(String[] args){
                   System.out.println("Outer classes");
                   Test1 t = new Test1();
                   Test1.Test2 t1 = t.new Test2();
                   System.out.println(t1.hno=10);
          }
}

Output –
Outer classes
10

Note – The applicable modifiers for outer classes are public, default, final, abstract, strictfp, but for inner classes the applicable modifiers are public, default, final, abstract, strictfp, private, protected, static.


2. Method local inner classes – Sometimes, we can declare a class inside a method and such type of inner classes are called as method local inner classes. The main objective of method inner class is to define method specific repeatedly requiring functionality. Method local inner class can fulfill the nested method requirement. We can access the method local inner class only within the method in which we have declare it (i.e.) we can’t access from outside of the method. Hence, this method local inner classes are most rarely used type of inner classes because of their lesser scope.

Example –
public class Test1 {
          public void m1()
          {
                   class Test2{
                             public void sum(int a,int b)
                             {
                                      System.out.println(a+b);
                             }
                   }
                   Test2 t1 = new Test2();
                   t1.sum(10, 20);
                   t1.sum(50, 60);
          }
          public static void main(String[] args){
                   Test1 t = new Test1();
                   t.m1();
          }
}

Output –
30
110


3. Anonymous Inner class – Anonymous class is one type of inner class which is a nameless class/interface. Like other class, it is not individual class. The main objective of the anonymous inner classes is for instant use. There are three types of anonymous inner classes. They are –
  • ·        Anonymous inner class that extends a class.
  • ·        Anonymous inner class that implements an interface.
  • ·        Anonymous inner class that defines inside method arguments.


Example –
class Test2{
          public void display()
          {
                   System.out.println("Test2 class display() method");
          }
}
public class Test1 {
          public static void main(String[] args){
                   Test2 t1 = new Test2(){
                             public void display()
                             {
                                      System.out.println("Anonymous display() method");
                             }
                   };
                   t1.display();
                   Test2 t2 = new Test2();
                   t2.display();
          }       
}

Output –
Anonymous display() method
Test2 class display() method

Explanation –
  • ·        Test2 t2 = new Test2(); here, we are just creating an object for Test2.
  • ·      Test2 t1 = new Test2(){ }; here, we create a child class for Test2 and for that child class we create an object with the parent reference.


Difference between General classes and Anonymous inner classes

Anonymous Inner class
General Class

It can extend only one class at a time.

A normal java class can also extend only one class at a time.

It can implement only one interface at a time.

It can implement any number of interfaces simultaneously.
It can extend a class or implement an interface but not both simultaneously.

It can extend a class and can implement any number of interfaces simultaneously.


4. Static Nested class – Sometimes, we can declare class with static modifiers. Such types of inner classes are called as static inner class. In normal classes, inner class object is always associated with outer class object. If the outer class object does not exist, there is no chance of inner class object. The static nested classes nested class object isn’t associated with outer class object, that is without existence of outer class object also there may be a chance of existence of static nested class object.

Example –
public class Test1 {
          static class Test2{
                   public void m1()
                   {
                             System.out.println("Static nested class method");
                   }
          }
          public static void main(String[] args){
                   Test1.Test2 t = new Test1.Test2();
                   t.m1();
                   //Static class without outer class
                   Test2 t1 = new Test2();
                   t1.m1();
          }       
}

Output –
Static nested class method
Static nested class method


Difference between Normal Inner class and Static Inner class

Normal Inner class
Static Nested class

Inner class object is always associated with outer class object i.e., without existence of outer class object there is no chance of existence of inner class object.

Static nested class object isn’t associated with outer class object i.e., without an existence of outer class object also there may be a chance of existence of static nested class object.

Inside normal inner classes, we can’t declare static members.

Inside static nested classes, we can declare the static members.
In normal inner class, we can’t declare main method and hence we can’t invoke inner class directly.
We can declare main method inside the static nested classes and hence we can invoke static nested class directly.


No comments:

Post a Comment