2

I am trying to understand advanced OOP concepts in C#. Lets say I have an interface and class that derived from this interface. Then, why do we need decleare interface as type while creating object or passing arguments to some method?

interface ILetter
{
   void Read();
}
class LoveLetter : ILetter
{
    public void Read(){
        Console.WriteLine("Love Letter Reading");
    }
}
class Program 
{
    static void Main(string[] args) 
    {
        ILetter myLetter = new LoveLetter();
        myLetter.Read();
        //Output : "Love Letter Reading"

        LoveLetter mySecondLetter = new LoveLetter();
        mySecondLetter.Read();
        //Output : "Love Letter Reading"
    }
}
Blithe
  • 105
  • 1
  • 9
  • 1
    Interface-based design (aka "design by contract") is an architectural technique, it helps to strongly decouple the implementation of LoveLetter. So changes to the implementation have very little impact on the rest of the program. Testing becomes much easier, friendly to dependency injection. Changes to the interface however tend to be pretty brutal, you have to think ahead carefully. Always hard to do and not very agile. https://en.wikipedia.org/wiki/Interface-based_programming – Hans Passant Apr 01 '18 at 09:41
  • Agree with @HansPassant. Dependency Injection is one the key thing here using Interface. It helps in writing Unit Test. Can read up on this too https://visualstudiomagazine.com/articles/2010/01/01/interface-based-programming.aspx – DriLLFreAK100 Apr 01 '18 at 09:50

5 Answers5

3

Accessing methods (which implements some functionality) via interface instead of concrete type gives you flexibility to change implementation of accessed functionality without changing client of that functionality.

Suppose we have ILogger interface

public interface ILogger
{
     void Log(string message);
}

And client of this interface

public class SomeService
{
    private readonly ILogger _logger; 

    public SomeService(ILogger logger)
    { 
        _logger = logger;
    }

    public void SomeMethod()
    {
        _logger.Log("Some message");
    }
 }

You can create concrete implementations of ILogger let's say ConsoleLogger and FileLogger and pass them into SomeService interchangeably, this way you will modify the outcome of SomeMethod ("Message" will be logged to Console or in File) without modifying SomeService. So, we are inverting the control using Dependency Injection.

tchelidze
  • 8,050
  • 1
  • 29
  • 49
1

In first case you are exposung only methods from the interface (recommended way) while in the latter one you expose everything public in the type.

Create one public method in the type and try to access him in both case and you will see what is the difference.

Its highly recommended that you program against abstractions than to concrete types.

kuskmen
  • 3,648
  • 4
  • 27
  • 54
1

Using an Interface allows flexibility, that means you are not depending on one particular class/implementation.

Let's say you have another letter class.

class LeaveApplicationLetter : ILetter
{
    public void Read(){
        Console.WriteLine("I'm on vacation");
    }
}

You can use other implementation of that interface.

class Program 
{
    static void Main(string[] args) 
    {
        ILetter myLetter = new LoveLetter();
        myLetter.Read();
        //Output : "Love Letter Reading"

        myLetter = new LeaveApplicationLetter ();
        myLetter.Read();
        //Output : "I'm on vacation"        

        LoveLetter mySecondLetter = new LoveLetter();
        mySecondLetter.Read();
        //Output : "Love Letter Reading"
    }
}

See this one for the deeper samples and explanation. C# interfaces - What's the point?

0

We don't say we 'derive' from an interface we say a class 'implements' an interface.

If we depend on the interface rather than the class it means our classes can deal with a more general case.

For example we could have a method to send a letter

 Send(ILetter message){....}

We could use this message to send any letter that implements ILetter. eg Class Bill : ILetter {...}

If we did not have the interface we would have to have

 Send(LoveLetter message){....}

 Send(Bill message){....}
Loofer
  • 6,841
  • 9
  • 61
  • 102
0

You have already gotten several answers that talk about the advantages of using interfaces, but none of them addresses an even simpler, much more basic point: you are asking about OOP, and classes aren't OOP in C♯, only interfaces are.

If you want to do object-oriented programming in C♯, you must use only interfaces as types. classes and structs define Abstract Data Types, not Object Types, only interfaces define Object Types.

I am trying to understand advanced OOP concepts in C#

This isn't even an advanced OOP concept in C♯, it is the very foundation of OOP in C♯, a very basic concept, maybe the most basic concept of all.

If you want more information, including examples, on the difference between Abstract Data Types and Objects, you can do no better than reading the brilliant On Understanding Data Abstraction, Revisited by William R. Cook. In the essay, Cook doesn't talk specifically about C♯, but what he says is easily transferrable to C♯.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653