This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Instantiating Interface or Abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiating Interface or Abstract" Watch "Instantiating Interface or Abstract" New topic

Instantiating Interface or Abstract

Arya Pramod

Joined: Jan 10, 2008
Posts: 12
Hi Everyone,

In Java, we cannot instantiate an abstract class or an interface using new.

But in the below code

I am able to instantiate the Interface as an anonymous class in the testMethod()Method which returns com.hi.test.Test$1.

Can anybody please explain how can we instantiate interface or abstract class using new operator

Thanks In Advance,
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You are implementing the interface, that's why it's ok. You could have :

You are not instantiating the interface, but you are instanciating an implementation of it. It's an anonymous implementation.

[My Blog]
All roads lead to JavaRanch
Jonny Andersson
Ranch Hand

Joined: Sep 07, 2004
Posts: 87
Or more exactly, you are creating a brand new class that is an anonymous inner class and which inherit (implements) the interface. It is called an anonymous inner class because it not has a name of its own and it can therefore not be instantiated explicitly without going through the method testMethod() and it can only be referenced through an reference variable with the type Interface.

This is an example why I like Java that much! It has a lot of useful constructs to support good object oriented design! With that said should anonymous inner classes be used with care to not caus code that is hard to read.

Truly agile Java application developer with Extreme Programming and high quality craftsmanship
Arya Pramod

Joined: Jan 10, 2008
Posts: 12
First of all I am sorry to post the same question in two places.

And Thanks to all for the reply.

It's true that in the code an implementation of the interface is getting instantiated. But what I am confused is that since we did'nt declare explicitly the implements keyword to implement the interface something like the below code

so how could the statement new Greetable() in the method is valid.

Or is it something that the class is implicitly inheriting the interface when an anonymoous class is being creating in the method ?
Ravi Nikam

Joined: Sep 28, 2007
Posts: 6
that statement is valid anyway because you are using curly braces({) before ending your statement with semicolon hence you are telling your compiler that, you are implementing interface here in curly braces(using anonymous class name). So it allows you to create instance of it.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3370

Originally posted by Arya Pramod:
..Or is it something that the class is implicitly inheriting the interface when an anonymoous class is being creating in the method ?

Yes, that is true. That is happening because of the interface name being specified and as explained by Jonny Andersson, it is the anonymous class being created on the fly!

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I agree. Here's the link:
subject: Instantiating Interface or Abstract