Anonymous Inner Classes in Java

We can also declare inner classes without name in Java, such type of inner classes are known as Anonymous Inner Classes in Java. The main purpose of anonymous inner classes is just for instance use (one time use ).

Types of Anonymous Inner Classes

Based on declration and behaviour anonymous inner classes are classified into three types.

  • Anonymous Inner classes that extend a class
  • Anonymous Inner classes that implement an interface.
  • Anonymous Inner classes that defined inside arguments.

Anonymous Inner Classes that Extends a Class

public class Vehicle {
    public void name(){
        System.out.println("car");
    }
}
 class TheCodeData{
     public static void main(String[] args) {
         Vehicle v1 = new Vehicle(){
             public void name(){
                 System.out.println("bus");
             }
         };
         v1.name();
         Vehicle v2 = new Vehicle();
         v2.name();
         System.out.println(v1.getClass().getName());
         System.out.println(v2.getClass().getName());
     }

 }
Anonymous Inner Classes that Extends a Class

In the above Java code

Vehicle v2 = new Vehicle();

Here we are creating a normal vehicle object.

Vehicle v1 = new Vehicle(){
             public void name(){
                 System.out.println("bus");
             }
         };
  • We are declaring a class that extends Vehicle class without name(Anonymous inner class).
  • For child class we are overriding name method.
  • For that child class we are creating an object with parent refrence.

Defining a Thread bu using Anonymous Inner class.

public class ThreadAnonymousInnerClass {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                for (int i=0;i<3;i++){
                    System.out.println("child Thread");
                }
            }
        };
        t.start();
        for(int i=0;i<3;i++){
            System.out.println("main thread");
            Thread.sleep(1000);
        }
    }
}

Anonymous Inner Class that Implements an Interface

Defining a Thread by Implementing Runnable Interface

public class ThreadByImplementingInterface {
    public static void main(String[] args) throws InterruptedException {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                for (int i= 0; 1 < 3; i++) {
                    System.out.println("Child Thread");
                }
                }
            };
        Thread t = new Thread(r);
        t.start();
        for(int i=0;i<3;i++){
            System.out.println("main method");
            Thread.sleep(1000);
        }
        }
    }

Anonymous Inner Class that define inside Arguments

public class AnonymousInnerCLass {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<3;i++){
                    System.out.println("Child Thread");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
        }
    });
        t.start();
        for(int i=0;i<3;i++){
            System.out.println("Main Thread");
            Thread.sleep(1000);
        }
    }
}
Anonymous Inner Class that define inside Arguments

Similar Java Tutorials

1 thought on “Anonymous Inner Classes in Java”

Leave a Comment