Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programming. I have seen some past questions about the purpose of interfaces. The answers I have seen are broadly two types:

1. Some answers will just explain what an interface is.
2. Other answers will say that an interface allows you to inherit from multiple classes, or to inherit by a structure rather than a class.

As far as I understand, implementing an interface does not really inherit anything from the interface because you have to write from scratch everything that you are supposedly inheriting from the interface. Since the interface has only signatures, it really has nothing you can inherit other than the title.

Since (as far as I know) you only inherit the name of the interface but nothing else, perhaps the real value of the interface is that it can mark a group of classes as having some common functionality?

Note: I asked this question before in "Stackoverflow" but I got only mocking and uncivil responses from people who probably did not understand the question. Please be better than that and refrain from uncivil language, even if you don't respect the question. My greatest respect to all the decent people here.

What I have tried:

C# interfaces such as ICommand and INotifyPropertyChanged.
Updated 28-Feb-17 17:06pm
[no name] 28-Feb-17 20:25pm    
Think of it as Interface = Contract. Knowing that a class implements a particular interface, you know that the class must have certain functionality. For instance, if you have a class that implements IDisposable, you know that the class has a mechanism in place to dispose of unmanaged resources that it knows of.
HD86_ 28-Feb-17 20:30pm    
Thanks. What you say makes sense to me.
HD86_ 1-Mar-17 0:45am    
NotPolicallyCorrect's comment answers my question completely. I am not interested in explanations about how the interface works. I only want to know the reason for why we use it.

1 solution

NotPolicallyCorrect's comment is correct - the hard part is, everyone 'groks' these sorts of things differently, and SO in particular like everyone to use 'the correct language/terms' - it took me ages to grok 'x => x % 2 == 0' for example, because it wasn't really a relevant example to me at that stage

- I think where you're coming from in your head 'having some common functionality' is ok, and that common functionality is 'the contract'

You said you'd tried "C# interfaces such as ICommand and INotifyPropertyChanged." - er, ok, they might have been a bit complex to start with - I'd hard started with something 'simpler' myself :-)

At the risk of un-doing all the good work so far, I'll present this example

// Interface 

public interface ILogger
    public void Log(string msg);

// 'Concrete' classes 

public class FileLogger : ILogger
      // TODO : Implement Log method to File, Possibly File Spec From Ctor

public class TextBoxLogger : ILogger
      // TODO : Implement Log method to TextBox, Possibly TextBox Name From Ctor

public class DatabaseLogger : ILogger
      // TODO : Implement Log method to Database, Possibly Database Connection From Ctor

So, you have the interface, ILogger, that says, any class that implements me MUST implement Log(string msg); because that's the 'contract' specified by the Interface definition

.. irrespective of whether you're using the FileLogger, TextBoxLogger, DatabaseLogger, once you have an instance of those objects, you only ever call Log("some message");

maybe that helps
Share this answer
HD86_ 1-Mar-17 0:00am    
Thanks Garth. Your example could not 'undo the good work' because this is a very simple example and I have already been working with more complex examples as I am creating a WPF app. I understand completely what an interface is and how it works, but I am only trying to understand the reason for why we use them.
Garth J Lancaster 1-Mar-17 0:52am    
ok, the why in the example I posed, is so my calling code only ever goes SomeLogger.Log("some message"); not have to figure out what sort of logger we are using and what the equivalent Log method is in that class
HD86_ 1-Mar-17 11:15am    
Thanks. This comment answers my question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900