Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating Interface or Abstract

 
Arya Pramod
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Pramod
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jonny Andersson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arya Pramod
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic