Anonymous Inner Class

  • Eclipse: Oxygen
  • Java: 1.8

Java anonymous inner class can be created in two ways, it can be abstract or concrete.

The class that has no name and that is used to overwrite an interface or an abstract method is called an anonymous inner class.

It is an inner class with no name and for which a single object is created. An anonymous inner class can be useful when creating an instance of an object with certain “extras”, such as the overload methods of a class or interface, without having to actually subclass a class.

It is useful for writing implementation classes for listener interfaces in graphics programming.

A class is created but its name is decided by the compiler which extends the lock class and provide the implementation of the isUnlocked method.

An anonymous class is created that is referred by lock variable of lock type. In order to get lock method, we are adding a getter method.

In the following program, we create an abstract class lock.

We create an object door to access the property of class door.  Whether the door is lock or not will be verified by if else statement along with passing the parameter through command line argument.

Argument Defined the Anonymous inner class.

Output

We are closed now, please visit later

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu