Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App Basic Android Programming Making a Button Do Something

Alexey Mironov
Alexey Mironov
587 Points

About OnClickListener

How did we create an instance of interface? Because in Java we can't create instance of interface.

vicente lee
vicente lee
6,271 Points
public interface SomeInterface {....}
public class SomeClass implements SomeInterface{}

Since we cannot create an instance of A, we need to do something like this:

SomeInterface foo = new SomeClass();  

2 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Alexey - We're not creating an instance of an interface. We're creating an instance of an anonymous class that implements the interface.

Take a look at this link to learn more about anonymous classes:

https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

If you put ({}) then it will work or try @overcaul (id, Rb)