Java Program to Create an Anonymous Class by Implementing an Interface

You are currently viewing Java Program to Create an Anonymous Class by Implementing an Interface

Java program to create an anonymous class by implementing an interface

// Java program to create an anonymous class by 
// implementing an interface

interface Sample {
  public void print();
}

class Demo {
  public void createAnonymousClass() {
    // Anonymous class by implementing 
    // Sample interface
    Sample s1 = new Sample() {
      public void print() {
        System.out.println("Inside the anonymous class.");
      }
    };
    s1.print();
  }
}

public class Main {
  public static void main(String[] args) {
    Demo d = new Demo();
    d.createAnonymousClass();
  }
}

Output

Inside the anonymous class.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply