What is an Argument-Defined Anonymous Inner Class in Java?

We highly recommend that you also read our article where we give an example of an anonymous inner class, or hopefully you already know what an anonymous inner class is.

In order to start our with our tutorial on argument defined anonymous inner classes in Java – which are also known as argument local anonymous inner classes – let’s start out with some example code:

interface ExampleInterface {
void interfaceMethod();
}

class ExampleClass {
  void exampleMethod(ExampleInterface e) {}
}

classSomeOtherClass {

void start(){
ExampleClass ex = new ExampleClass();
ex.exampleMethod (What should go in here?  );

}

}


In the code above we have an interface called ExampleInterface that has just one method called interfaceMethod. And, we have another class called ExampleClass that just has one method called exampleMethod that accepts an object of type ExampleInterface (which is an interface) as an argument.

The question here – which we highlighted in red above – is if we want to call the exampleMethod method from another class, how do we invoke that method without an object that has the same type as the interface ExampleInterface?

Well, one obvious option is to create a new, separate class that implements the interface, so something like this:

class InterfaceImplementer implements ExampleInterface {

void interfaceMethod()
{
//provide a definition here as well...

}

}

Then, we can just create an object of the InterfaceImplementer class and pass that into our call to exampleMethod. So, it would look something like this:

interface ExampleInterface {
void interfaceMethod();
}

class ExampleClass {
  void exampleMethod(ExampleInterface e) {}
}

classSomeOtherClass {

void start(){
ExampleClass ex = new ExampleClass();
InterfaceImplementer intImp = new InterfaceImplementer();

ex.exampleMethod (intImp);

}

}

Example of using an Argument Defined Anonymous Inner Class

But, as you probably guessed by the title of this article, another option is to use an Argument Defined Anonymous Inner Class instead. How does that work? Well, it’s quite simple – we just define the anonymous inner class directly inside the argument. You read that correctly – we are essentially defining and instantiating a class right inside an argument. Here is what the syntax for an argument defined anonymous inner class looks like, building on our previous example:

interface ExampleInterface {
void interfaceMethod();
}

class ExampleClass {
  void exampleMethod(ExampleInterface e) {}
}

classSomeOtherClass {

void start(){
ExampleClass ex = new ExampleClass();
ex.exampleMethod (new ExampleInterface () {
public void interfaceMethod() {
System.out.println("Hello!");
} //end interfaceMethod
}//end anonymous inner class definition
  ); //end argument and exampleMethod call
}

}

How does an Argument Defined Anonymous Inner Class work?

In the example code above, we create an instance of a class that implements the ExampleInterface interface, and we override the interfaceMethod as well. And, we do all of this inside an argument to a method! Amazing right? You might be confused, so let’s talk about this some more.

The key thing that you must understand is that we are creating both an implementationand an instance of a class that implements the ExampleInterface interface. Note that we are NOT instantiating the interface directly, even though the code may appear to be doing that – we are actually defining a class with no name (which is why it is called ANONYMOUS) that also implements the ExampleInterface interface. And then we create an instance of this class.

You might want to read this article as well to see another example of an anonymous class that implements an interface : Anonymous inner class implements interface.

Argument local anonymous inner class versus anonymous inner class

Also note that an argument defined anonymous inner class has syntax that ends like this:

});

But normal anonymous inner classes will end like this:

};

Reference: http://www.programmerinterview.com/index.php/java-questions/argument-defined-anonymous-inner-class/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值