• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

can we creat an instance of an interface?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we create an instace of an interface? do inteface have constructors?


 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr Gavin
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actual, I phrased that badly.
The default constructor will only be created by the compilor if no other constructors exist for the class.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But as Gavin said, you can create anonymous classes that implement one single interface / extend one single (abstract) class.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Max
 
reply
    Bookmark Topic Watch Topic
  • New Topic