Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

instance of an interface

  Asked By: Aditi    Date: Nov 14    Category: Java    Views: 749
  


is there any way that an interface can instantiate? how?
if it can be done...then what property that object would hold?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Arthur Cole     Answered On: Nov 14

No, you can not instantiate an interface. You can only insantiate
implementation classes.

 
Answer #2    Answered By: Jim Williamson     Answered On: Nov 14

Yes, We cannot instantiate interface  directly
Indirectly we can create an object  of the interface.

e.g.

public interface inf {
public String get();
}

public class c implements inf{
public String get(){
return "worldofharry";
}
}

Now to create the object of interface by

inf inter = (inf)new c();

 
Answer #3    Answered By: Sherri Parker     Answered On: Nov 14

is there any way that an interface  can instantiate? how?
if it can be done...then what property  that object  would hold?

There is no way the you can instantiate an interface or an abstract class.
You can only instantiate the class implementing the interface or a class
that extends abstract class.

say you have an interface "InterfaceOne" as follows

public interface InterfaceOne
{

public void methodOne();

}


then you have to write a class as follows

public class InterfaceOneImpl implements InterfaceOne
{
public void methodOne()
{
// logic goes here
}
}

Then you can have a statement


InterfaceOne intf=new InterfaceOneImpl();
intf.methodOne();

 
Didn't find what you were looking for? Find more on instance of an interface Or get search suggestion and latest updates.




Tagged: