Mawia
Mawia

Reputation: 4310

Concept name required

I can't remember the name of this concept.

  public interface MainInterface {
      public void method1();
      public void method2();
 }

  void testMethod() {
        methodMain(new MainInterface() {

            @Override
            public void method1() {
                System.out.println("This is method1");
            }

            @Override
            public void method2() {
                System.out.println("This is method2");
            }
        });
    }

   void methodMain(MainInterface mi) {
        mi.method1();
        mi.method2();
   }

What is this concept and how exactly it works?

Upvotes: 0

Views: 65

Answers (3)

sp00m
sp00m

Reputation: 48807

Anonymous classes is what you search.

Upvotes: 0

Costi Ciudatu
Costi Ciudatu

Reputation: 38195

The concept is referred to as callback. In java, you only have callback interfaces, in other languages you can have callback functions.

As a design pattern, this concept is involved in the strategy pattern (as amit already mentioned).

Upvotes: 0

codebox
codebox

Reputation: 20254

You have created an instance of an Anonymous Inner Class (i.e. a class without a name).

Upvotes: 3

Related Questions