How to implement an interface using an anonymous inner class in Java?

JavaObject Oriented ProgrammingProgramming

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
raja
Published on 24-May-2019 10:14:15
Advertisements