Anonymous inner classes

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;
/**
 * 
 * @author ashok.mariyala
 *
 */
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.

package com.ashok.innerclasses;

/**
 * 
 * @author ashok.mariyala
 *
 */
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;

/**
 * 
 * @author ashok.mariyala
 *
 */
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;
/**
 * 
 * @author ashok.mariyala
 *
 */
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
Anonymous inner classes


Scroll to top