| 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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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: 19216
|
|
|
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
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
|
 |
 |
|
|
subject: can we creat an instance of an interface?
|
|
|