File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes can we creat an instance of an interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we creat an instance of an interface?" Watch "can we creat an instance of an interface?" New topic
Author

can we creat an instance of an interface?

Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38
Can we create an instace of an interface? do inteface have constructors?


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Chitta Ranjan Mahato wrote:Can we create an instace of an interface? do inteface have constructors?


Yes you can create an instance of an interface using the anonymous inner class syntax and providing implementation of all the methods of the interface. Interfaces and Anonymous inner classes both can't have a constructor. But you can create an instance initializer block in the anonymous inner class.

Chitta Ranjan Mahato wrote:Please Explain this code.


Where is the code??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
I would say, technically you can NEVER create an instance of an interface, however you can implement an interface. The method described above is simply creating an implementation of the interface, not an instance of the interface.

Remember that a class that implements an interface should pass the IS-A test for that interface.
Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38

interface sweety
{
public String toString();

}
public class Main implements sweety {


public static void main(String[] args) {

System.out.print(new sweety(){public String toString(){return "SweetHeart";}});
}

}
output: SweetHeart
In the above code from where the java compiler find new sweety()
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Ok, firstly you really should use code tags when posting code.

Types, including interface types should start with a capital letter (i.e Sweety).

You are defining in an interface a method called toString(), which ever Object will have.

There is no need for the Class containing the main method to implement sweety.

As to where the constructor comes from, my guess is that you are creating an annoymous implementation of the sweety interface.

Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Every class has a default constructor, which has no parameters, you are creating an annoymous inner class of type Sweety.
The compilor knows this and will create a default constructor for your annoymous inner class.

That is how i see this.
Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38
Thanks Mr Gavin
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Actual, I phrased that badly.
The default constructor will only be created by the compilor if no other constructors exist for the class.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Chitta Ranjan Mahato wrote:Can we create an instace of an interface? do inteface have constructors?


No, you cannot create an instance of an interface, nor can you create an instance of an abstract class. I.e., you cannot use the new keyword with an interface or abstract class. Interfaces do not have constructors.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

But as Gavin said, you can create anonymous classes that implement one single interface / extend one single (abstract) class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Rob Prime wrote:But as Gavin said, you can create anonymous classes that implement one single interface / extend one single (abstract) class.


Yeah. In fact, you have to create a class that implements them. Raising anonymous inner classes only confuses the issue, since the implementing class is hidden. It may look like you're creating an instance of the interface, but of course, you're not. The basic simple fact is that you cannot create an instance of an interface or abstract class.
Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38
Thanks Max
 
jQuery in Action, 2nd edition
 
subject: can we creat an instance of an interface?