Anonymous class in java



Anonymous class used to define the method , when you are create the object. It mostly used in to define the interface method in without creating the another class.








Example-1
class A
{
      public void show()
      {
            System.out.print("Class A");
      }
}

public class Main
{
      public static void main(String[] args) 
      {
            A obj = new A()
            {
                  public void show()
                  {
                        System.out.print("Anonymous Class");
                  }
            };

            obj.show();
      }
}


Output



Anonymous Class







Example-2
interface A
{
      public void show();
}

public class Main
{
      public static void main(String[] args) 
      {
            A obj = new A()
            {
                 public void show()
                 {
                      System.out.print("mycrazycoding");
                 }
            };
            
            obj.show();
      }
}


Output



mycrazycoding