Forums Register Login

can we creat an instance of an interface?

+Pie Number of slices to send: Send
Can we create an instace of an interface? do inteface have constructors?


+Pie Number of slices to send: Send
 

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??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

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()
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Mr Gavin
+Pie Number of slices to send: Send
Actual, I phrased that badly.
The default constructor will only be created by the compilor if no other constructors exist for the class.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
But as Gavin said, you can create anonymous classes that implement one single interface / extend one single (abstract) class.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thanks Max
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12250 times.
Similar Threads
about interface in jsp
Can we implement an interface in jsp ? if not why?
polymorhism
Interface in Jsp
Extend an Interface (Not implement)?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:46:26.