Implement Interface Using Anonymous Inner Class in Java



An anonymous inner class is a class which doesn't have a name, we will directly define it at the instantiation line.

Example

In the following program, we are implementing the toString() method of TutorialsPoint interface using Anonymous inner class and, print its return value.

interface TutorialsPoint{
   public String toString();
}
public class Main implements TutorialsPoint {
   public static void main(String[] args) {
      System.out.print(new TutorialsPoint() {
         public String toString() {
            return "Welcome to Tutorials Point";
         }
      });
   }
}

Output

Welcome to Tutorials Point
Updated on: 2023-11-17T08:41:15+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements