Recent Posts

Sunday, 28 August 2016

Inner Classes Tutorial Part 2


2. Method local inner classes 
1. Sometimes we can declare a class inside a method such type of inner classes are called method local inner classes.
2. The main objective of method local inner class is to define method specific repeatedly required functionality.
3. Method Local inner classes are best suitable to meet nested method requirement.
4. We can access method local inner class only within the method where we declared it. That is from outside of the method we can't access. As the scope of method local inner classes is very less, this type of inner classes are most rarely used type of inner classes.
E.g
package com.ashok.innerclasses;

public class OuterClass {
   public void method() {
      class InnerClass {
         public void sum(int a, int b) {
            System.out.println("The sum is : " + (a + b));
         }
      }
      InnerClass i = new InnerClass();
      i.sum(5, 10);
      i.sum(50, 200);
   }

   public static void main(String[] args) {
      new OuterClass().method();
   }
}
Output
The sum is : 15
The sum is : 250
* If we are declaring inner class inside instance method then we can access both static and non static members of outer class directly.
* But if we are declaring inner class inside static method then we can access only static members of outer class directly and we can't access instance members directly.
package com.ashok.innerclasses;

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

   public void methodOne() {
      class InnerClass {
         public void methodTwo() {
            System.out.println(str);
            System.out.println(i);
         }
      }
      InnerClass in = new InnerClass();
      in.methodTwo();
   }

   public static void main(String[] args) {
      new OuterClass().methodOne();
   }
}
Output
Waytoeasylearn
100
* If we declare methodOne() method as static then we will get compile time error saying "non-static variable x cannot be referenced from a static context".
* From method local inner class we can't access local variables of the method in which we declared it. But if that local variable is declared as final then we won't get any compile time error.

3. Anonymous inner classes
* Sometimes we can declare inner class without name such type of inner classes are called anonymous inner classes.
* The main objective of anonymous inner classes is "just for instant use".
* There are 3 types of anonymous inner classes
1. Anonymous inner class that extends a class.
2. Anonymous inner class that implements an interface.
3. Anonymous inner class that defined inside method arguments.
package com.ashok.innerclasses;

class PopCorn {
   public void taste() {
      System.out.println("Pop Corn is Spicy");
   }
}

public class Test {
   public static void main(String[] args) {
      PopCorn p = new PopCorn() {
         public void taste() {
            System.out.println("Pop Corn is Salty");
         }
      };
      p.taste();
      PopCorn p1 = new PopCorn();
      p1.taste();
   }
}
Output
Pop Corn is Salty
Pop Corn is Spicy
PopCorn p=new PopCorn();
     We are just creating a PopCorn object.

PopCorn p=new PopCorn() {

};
     We are creating child class without name for the PopCorn class and for that child class we are creating an object with Parent PopCorn reference.
PopCorn p=new PopCorn() {
   public void taste() {
      System.out.println("salty");
   }
};
1. We are creating child class for PopCorn without name.
2. We are overriding taste() method.
3. We are creating object for that child class with parent reference.
Note
     Inside Anonymous inner classes we can take or declare new methods but outside of anonymous inner classes we can't call these methods directly because we are depending on parent reference.[parent reference can be used to hold child class object but by using that reference we can't call child specific methods]. These methods just for internal purpose only.
E.g
package com.ashok.innerclasses;

class PopCorn {
   public void taste() {
      System.out.println("Pop Corn is Spicy");
   }
}

public class Test {
   public static void main(String[] args) {
      PopCorn p = new PopCorn() {
         public void taste() {
            methodOne();
            System.out.println("Pop Corn is Salty");
         }

         public void methodOne() {
            System.out.println("Child specific method");
         }
      };
      // p.methodOne(); // C.E 
      p.taste(); // Pop Corn is Spicy
      PopCorn p1 = new PopCorn();
      p1.taste(); // Pop Corn is Salty
   }
}
Anonymous Inner Class that implements an interface
package com.ashok.innerclasses;

public class Test {
   public static void main(String[] args) {
      Runnable r = new Runnable() {
         public void run() {
            for (int i = 0; i < 5; i++) {
               System.out.println("Child thread");
            }
         }
      };
      Thread t = new Thread(r);
      t.start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main thread");
      }
   }
}
Output
Main thread
Main thread
Main thread
Main thread
Main thread
Child thread
Child thread
Child thread
Child thread
Child thread
Anonymous Inner Class that define inside method arguments
package com.ashok.innerclasses;

class Test {
   public static void main(String[] args) {
      new Thread(new Runnable() {
         public void run() {
            for (int i = 0; i < 5; i++) {
               System.out.println("Child thread");
            }
         }
      }).start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main thread");
      }
   }
}
Output
main thread
main thread
main thread
main thread
main thread
child thread
child thread
child thread
child thread
child thread


Next Tutorial  Inner Classes Tutorial Part 3

Previous Tutorial  Inner Classes Tutorial Part 1

No comments:

Post a Comment