Bharadwaj
Bharadwaj

Reputation: 2553

Object creation with interface

In C# we can inherit multiple interfaces. In such scenario if an object is created will it just hide the properties or methods or will the compiler avoids creation and memory allocation to those from other interface? For example, I have two interfaces and a class like below

    interface IExample1
    {
        void E1Test1();
        void E1Test2();
    }

    interface IExample2
    {
        void E2Test1();
        void E2Test2();
    }

    class Example : IExample1, IExample2
    {
        public void E1Test1()
        {
            throw new NotImplementedException();
        }

        public void E1Test2()
        {
            throw new NotImplementedException();
        }

        public void E2Test1()
        {
            throw new NotImplementedException();
        }

        public void E2Test2()
        {
            throw new NotImplementedException();
        }
    }

When I create objects like,

IExample1 IE1 = new Example();
IExample2 IE2 = new Example();

Will the compiler creates the complete object for both interfaces including all methods and hide it for one which don't have access? or it just creates with the methods which are available for a particular interface?

Upvotes: 3

Views: 166

Answers (3)

Diizzy
Diizzy

Reputation: 554

The methods belong to an instance of a class. That instance is the object. You can only create a new instance from a concrete type(like a class for example). You can not create a new instance from an abstract type(Abstract class or Interface).

Thus the conclusion, when you create with new (), you will have all methods in the new instance.

Further: Your new object(as in your example) can be seen in 3 ways:

  1. Directly through the type(Example)
  2. Though an interface(IExample1)
  3. Through the other interface(IExample2)
    Case 1) You see everything that is declared on the class(lets suppose everything is public :) )
    Case 2) Even though you create new Example(), if the runtime type is IExample1 you will see only what is declared in this interface
    Case 3) Even though you create new Example(), if the runtime type is IExample2 you will see only what is declared in this interface

Conclusion: The object you created is the same one(new Example()) but only the way you look at it changes.

Tip: read about implicit and explicit interface implementation

Upvotes: 2

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37050

When you instantiate a class using its constructor (which is after using new ...) there´s always a complete instance of that clas in memory. There´s no such thing as an instance of an interface.

An interface on the other side is just some kind of a view upon that instance. So in your example you have four methods that all exist on your instance. However you may or may not access those methods from a reference of either type IExample1 or IExample2. E.g. you cannot E2Test1 on a reference of type IExample1, although the underlying instance does have this method:

IExample1 i1 = new Example();

Now calling i1.E1Test1() will compile just fine, while i1.E2Test1() will not, as that method is defined on another interface. However you can simply cast the instance which is referenced by i1 to the other interface and access that member this way:

IExample i2 = (IExample2) i1;
i2.E2Test1();

EDIT as of TimSchmelters comment: If your Example-class would implement only IExample1 and not the other interface casting to that one would surely fail and produce an InvalidCastException at runtime. So in that case instances of Example-class will only have the members defined on IExample1, not on IExample2. Casting to the latter makes no sense then.

Upvotes: 8

Daniel A. White
Daniel A. White

Reputation: 190986

You are creating an Example object on the heap. It has everything that an Example has. For instance you can do this.

IExample1 IE1 = new Example();
IExample2 IE2 = new Example();
IExample2 casted = (IExample1)IE1;

You can also debug and see that IE1 and IE2 are in fact Example instances.

Upvotes: 3

Related Questions