Recent Posts

Saturday, 27 August 2016

Inner Classes Tutorial


Introduction
     We can declare a class inside another class such type of classes are called inner classes. This inner classes concept has introduced in 1.1 version as the part of event handling. By observing the utilities and functionalities of inner class slowly the programmers are using this concept even in regular coding also. Without existing one type of object if there is no chance of existing another type of object then we should go for inner classes.
E.g 1
     With out existing car object there is no chance of existing wheel object. Hence we have to declare wheel class inside car class.
class car {
   class wheel {
   
   }
}
E.g 2
     A map is a collection of entry objects. With out existing Map object there is no chance of existing entry object. Hence we have to define entry interface inside map interface.
interface Map {
   interface Entity {

   }
}
     Based on the purpose and position of inner class all the inner classes are divided into 4 categories.
1. Normal/Regular inner classes
2. Method local inner classes
3. Anonymous inner classes
4. static nested classes

1. Normal (or) Regular inner class
     If we are declaring any named class inside another class directly without static modifier such type of inner classes are called normal or regular inner classes.
E.g
class OuterClass {
   class InnerClass {

   }
}
     When you compile the above code, compiler generates two classes named OuterClass.class and OuterClass$InnerClass.class.
class OuterClass {
   class InnerClass {
   }
   public static void main(String[] args) {
      System.out.println("OuterClass main method");
   }
}

java OuterClass 
OuterClass main method

java OuterClass$InnerClass
Exception in thread "main" java.lang.NoSuchMethodError:main
     Inside inner class we can't declare static members. Hence it is not possible to declare main() method and we can't invoke inner class directly from the command prompt.

Accessing inner class code from static area of outer class
E.g
package com.ashok.innerclasses;

public class OuterClass {
   class InnerClass {
      private void m1() {
         System.out.println("Inside inner class");
      }
   }

   public static void main(String[] args) {
      OuterClass out = new OuterClass();
      OuterClass.InnerClass in = out.new InnerClass();
      in.m1();
      // Or
      OuterClass.InnerClass inn = new OuterClass().new InnerClass();
      inn.m1();
      // Or
      new OuterClass().new InnerClass().m1();
   }
}
Output
Inside inner class
Inside inner class
Inside inner class
Accessing inner class code from instance area of outer class
package com.ashok.innerclasses;

class OuterClass {
   class InnerClass {
      public void inner() {
         System.out.println("Inside InnerClass class method");
      }
   }

   public void outer() {
      InnerClass in = new InnerClass();
      in.inner();
   }

   public static void main(String[] args) {
      OuterClass out = new OuterClass();
      out.outer();
   }
}
Output
Inside InnerClass class method
Accessing inner class code from outside of outer class
package com.ashok.innerclasses;

class OuterClass {
   class InnerClass {
      public void inner() {
         System.out.println("Inside InnerClass method");
      }
   }
}

public class Test {
   public static void main(String[] args) {
      new OuterClass().new InnerClass().inner();
   }
}
Output
Inside InnerClass method
     From inner class we can access all members of outer class (both static and non-static, private and non private methods and variables) directly.
package com.ashok.innerclasses;

public class OuterClass {
   String str = "Waytoeasylearn";
   static int y = 100;

   class InnerClass {
      public void inner() {
         System.out.println(str);// 10
         System.out.println(y);// 20
      }
   }

   public static void main(String[] args) {
      new OuterClass().new InnerClass().inner();
   }
}
Output
Waytoeasylearn
100
     Within the inner class "this" always refers current inner class object. To refer current outer class object we have to use "outer class name.this".
E.g
package com.ashok.innerclasses;

public class OuterClass {
   int x = 10;
   class InnerClass {
      int x = 100;
      public void inner() {
         int x = 1000;
         System.out.println(x);// 1000
         System.out.println(this.x);// 100
         System.out.println(OuterClass.this.x);// 10
      }
   }
   public static void main(String[] args) {
      new OuterClass().new InnerClass().inner();
   }
}
Output
1000
100
10
     The applicable modifiers for outer classes are
1. public
2. default
3. final
4. abstract
5. strictfp
     But for the inner classes in addition to this the following modifiers also allowed.
1. private
2. protected
3. static [static nested classes]

Nesting of Inner classes
     We can declare an inner class inside another inner class.
E.g
package com.ashok.innerclasses;

public class OuterClass {
   class A {
      class B {
         private void method() {
            System.out.println("Welcome to Waytoeasylearn");
         }
      }
   }
 
   public static void main(String[] args) {
      OuterClass out = new OuterClass();
      OuterClass.A a = out.new A();
      OuterClass.A.B b = a.new B();
      b.method();
   }
}
Output
Welcome to Waytoeasylearn

Next Tutorial  Inner Classes Tutorial Part 2

Previous Tutorial  Garbage Collection Tutorial

No comments:

Post a Comment