Basic of Java

OOPs Concepts

Java String

Java Regex

Exception Handling

Java Inner Class

Java Anonymous inner class

A class that have no name is known as anonymous inner class in java. It should be used if you have to override method of class or interface. Java Anonymous inner class can be created by two ways:

  1. Class (may be abstract or concrete).
  2. Interface

Java anonymous inner class example using class

Flash Soon


Output:

Internal working of given code

Flash Soon


  1. A class is created but its name is decided by the compiler which extends the Person class and provides the implementation of the eat() method.
  2. An object of Anonymous class is created that is referred by p reference variable of Person type.

Internal class generated by the compiler

Flash Soon


Java anonymous inner class example using interface

Flash Soon


Output:

Internal working of given code

It performs two main tasks behind this code:

Flash Soon


  1. A class is created but its name is decided by the compiler which implements the Eatable interface and provides the implementation of the eat() method.
  2. An object of Anonymous class is created that is referred by p reference variable of Eatable type.

Internal class generated by the compiler

Flash Soon



Copyright 2017 Design& Development