What happens when we create an object of interface?

45,728

Solution 1

Actually you cannot create an instance of an interface.

You create an instance of some class, implementing the interface. Actually there can be dozens of classes, implementing one interface. So when you use a variable of interface type, the only thing you are guaranteed is that the object, which is in fact referenced by the variable, implements the interface and you can use any of the interface methods, properties, etc.

interface IFoo
{
   void DoFoo();
}

class Foo1: IFoo
{
    public DoFoo()
    {
        //one implementation
    }
}

class Foo2: IFoo
{
    public DoFoo()
    {
        //the other implementation
    }
}

IFoo tmp = new Foo1();
tmp = new Foo2();

You may see a deep explanation in SO: Using Interface variables

Solution 2

can somebody please explain what actually happens when we create an object of interface?

An analogy I like to use is that an interface is like a mask - when you create an instance of a class that implements an interface (e.g. IFoo), then treat the class as the interface then the interface is like a mask that an actor would wear - the class appears to be and acts as the interface (mask) even though there is a whole bunch of stuff under the mask (interface).

but can't really grasp the logic of Why C# allows creation of an object(instance) of interface?

The other thing to mention is that an interface is a contract - whatever methods / properties / etc are defined on the interface are guaranteed to be implemented on the class. Classes that are completely and utterly different to each other can implement the same interface - when you have an instance of each class and treat it (cast it) as the interface then the same methods can be called on each class, although the implementation (actual code) in each class can be wildly different.

Example:
The classes Car and Human both implement the interface IMove, and IMove defines the method void Move();. They are both totally unrelated classes with no common ancestry, but if you have an instance of Car and Human, then they are both guaranteed to have the method void Move(), although the code in that method will differ between then them as they move in totally different ways. If you then cast these different objects to the same interface (var x = (IMove) someCar or var x = someHuman as IMove) then you can treat the object reference identically.

Interfaces and abstract classes have some similarities - they both specify things in a contractual way (the extender or implementor has to implement specific things), and they both cannot be created directly (the difference is that abstract classes can provide some implementation whereas interfaces cannot, and interfaces can only be implemented, never inherited).

If you are wondering what happens under the hood at MSIL level then there are other far more qualified people around here who can elaborate on that.

Solution 3

Interface is that, just an interface. You can not instantiate an interface. You can use it as a variable which points to a class which implements that interface. Interface is a public collection of methods/properties with a guarantee that all of its methods are implemented. Abstract classes are similar to interfaces but it does not provide such guarantee.

In OOP, this is a kind of polymorphism and there are other types of polymorphisms.

Share:
45,728
B-Abbasi
Author by

B-Abbasi

I am an entrepreneur, software developer, book reader and a psychology student :)

Updated on June 26, 2020

Comments

  • B-Abbasi
    B-Abbasi almost 4 years

    I am new to interfaces in C#. So can somebody please explain what actually happens when we create an object of interface?

    I know why we have interfaces in other languages but can't really grasp the logic of Why C# allows creation of an object(instance) of interface? If interfaces do not have function definitions or variables then how can an object be created?

    I have been searching on forums but couldn't get the point. here is a tutorial i found on interfaces http://www.c-sharpcorner.com/uploadfile/6897bc/interfaces-in-C-Sharp/ if you visit the link you can see that first the writer makes object of a class and than an object of inteface. When he writes

    Interface_object = class_object;    
    

    interface object extracts the features from class object...
    How and why does it happen if there is no implementation and variable in the interface ?