10
Reply

Can we create instance of an interface? If yes, How?

Sahil Sharma

Sahil Sharma

10y
7k
1
Reply

    No

    Yes , we create of instance of Interface. Interface L1 { void Add(); } class ABC:L1 { void Add() { Console.writeline("H r u"); } } L1 obj=new ABC(); Console.writeline(obj.Add());

    We can't create instance of interface which holds reference of it's own type. interface IExample {void abc(); } IExample obj=new IExample();// Not PossibleBut Interface object can hold reference of its derived class.public class Example:IExample {public void abc(){some code;} }IExample obj=new Example();// Possible and correct

    You can't Create Instance of Interface,But you can create reference to the Interface by using Child class reference objectlike ParentInterface objParent=new ChildClassName(); objParent.ParentInterfaceMethodshere();

    interface can not be instantiated,but instance of interface can also be created provided the body is added to the interface at the time of instantiation which normally class provides; interface iabc {}

    Interface can not be directly instantiated. We can create an instance of a class that implements the interface, then assign that instance to a variable of the interface type. IControl c= new DemoClass();

    Interface1 iterfaceObject = new Class1(); //can do this

    @Himanshu Tomar: I have the same code but still confused about the implementation. See the link: http://www.c-sharpcorner.com/Forums/Thread/267281/

    public interface Interface1 { void ImplementLogic(); } public class Class1: Interface1 { public void ImplementLogic() { // Implement your logic } } public class Class2 { // Creating Instance of Interface Interface1 iterfaceObject = new Class1(); }

    Cannot create an instance of the abstract class or interface.