Quantcast
Channel: I think I missed something on the "Programming to an interface" concept - Stack Overflow
Viewing all articles
Browse latest Browse all 7

I think I missed something on the "Programming to an interface" concept

$
0
0

So I am still very new to C# and using interfaces, and when I thought I understood them I realized I don't completely. The confusion I have found that I am seeking some clarification here for is, when you create an interface, and have a class inherit from it

public Interface ISomeInterface{  //some methods/properties}public class FooClass : ISomeInterface{  //implemented ISomeInterfaces methods/properties}

And you use this class object in an implementation somewhere in your program

public class BarClass {  private ISomeInterface _someInterface;  public BarClass(ISomeInterface someInterface)  {    _someInterface = someInterface;  }  //rest of class}

My confusion is why do I see it setup this way. I thought that I would have instantiated a new object of type FooClass, as well as used an object of type FooClass in the constructor as such:

public class BarClass {  private FooClass _fooClass;  public BarClass(FooClass fooClass)  {    _fooClass = fooClass;  }  //rest of class}

What am I missing to understanding this? I didn't think I would directly be declaring objects of an Interface?

Thanks in advance.


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>