AppDividend
Latest Code Tutorials

Java Anonymous Inner Class Tutorial With Example

0

Java Anonymous Inner Class Tutorial With Example. The anonymous inner class in Java is an Inner class (non-static nested class) which does not have any name. It is used in java to override the methods of interface and classes. Unlike other classes, it does not have any constructor (as the name of a constructor and the name of the class must be the same, but the anonymous inner class doesn’t have a name). It can be created either with an interface or with a class.

Java Anonymous Inner Class

The anonymous inner class can extend only one class or can implement only one interface, unlike other classes which can extend a class and implement several interfaces simultaneously. There will be only one object created for an anonymous inner class. The expression for an anonymous inner class consists of:

  1. Name of the interface going to be implemented or the class name going to be extended.
  2. The new operator.
  3. Parenthesis for the arguments of the constructor of the enclosing class.
  4. The body containing method declarations.

The anonymous inner class can access the members of the enclosing class. It can also access the local variables in the enclosing block provided that they are declared as final.

It can contain static members that are constants. The anonymous inner class can have methods, but statements are not allowed inside the body.

When a nested class is compiled, two class files get created in the system having names as OuterClass.class and Outerclass$NestedClass.class. The anonymous inner class is named as OuterClass$1.class.

#Different kind of Nested classes

See the following diagram.

 

Java Anonymous Inner Class

#Expression

See the following syntax.

OuterClass objectname = new OuterClass() 
{     
      //method declarations and data members
};

See the following program.

We can also have the anonymous inner class that implements the interface. Following is the program to show the implementation of an anonymous inner class using an interface.

//interface containing two abstract methods
interface Animals {
  public void showPets();
  public void showWild();
}

public class Example1 {
  public static void main(String[] args) {
    // creating anonymous inner class implementing the interface
    Animals animal = new Animals() {
      // body of anonymous inner class
      // implementing the abstract methods of the interface
      public void showPets() {
        System.out.println("Pet Animals Are: DOGS, CATS");
      }

      public void showWild() {
        System.out.println("Wild Animals Are: LION, TIGER, WOLF");
      }
    };

    // calling the methods
    animal.showPets();
    animal.showWild();
  }
}

See the following example.

 

Anonymous Inner Class

In this program, the anonymous inner class is used to implement an interface named Animals. The interface has two abstract methods, and this anonymous class is used to implement these methods. 

See the following Program to show the implementation of the anonymous inner class using a concrete class.

//creating a class with some methods
class Cars {
  void color() {
    System.out.println("color of the car: RED");
  }

  void mileage() {
    System.out.println("mileage of the car: 28 kmpl");
  }
}

public class Example2 {
  public static void main(String[] args) {
    // creating the anonymous class and overridding some methods
    Cars ford = new Cars() {
      void mileage() {
        System.out.println("mileage of the car: 16 kmpl");
      }
    };

    System.out.println("Methods called by Anonymous class object: ");
    ford.color(); // not overridded
    ford.mileage();

    // creating normal object and calling its methods
    System.out.println("\nMethods called by Normal class object: ");
    Cars defaultCar = new Cars();
    defaultCar.color();
    defaultCar.mileage();
  }
}

See the following output.

 

Java Anonymous Inner Class Tutorial

In this program, the anonymous inner class is used to override some methods of a concrete class named Cars. The concrete class is containing 2 methods, and this anonymous class is used to override one of these methods. 

See the Program to show the implementation of the anonymous inner class inside the arguments of a method.

//creating an interface with an abstract method
interface Salutation {
  public void morning();
}

// creating a class with method invoking Salutaion's method
class WishAll {
  public void greet(Salutation s) {
    s.morning();
  }
}

public class Example3 {
  public static void main(String[] args) {
    // object for WishAll class
    WishAll wish = new WishAll();

    // creating anonymous class inside the method argument of WishAll class.
    // it will implement the Salutation interface
    wish.greet(new Salutation() {
      public void morning() {
        System.out.println("Wishing all a very good morning!");
      }
    });
  }
}

See the following output.

 

Anonymous Inner Class Tutorial With Example

In this program, the anonymous inner class is created inside the method argument of the class WishAll. The method takes an object of the interface Salutation as its argument, so implementation of this anonymous inner class implements the interface and its abstract method.

Finally, Java Anonymous Inner Class Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.