-2

I have been struggling to understand what an interface actually does. Following is a very clear example that should be clear to other beginners.

Could somone confirm (or not) that the interface does nothing? It acts like an elaborate comment? Moreover, if you used the same interface in two classes, you would have to define the functions and this might be un-DRY? Or have I missed something?

<?php
interface fax{
   public function dial();
   public function send();
   public function recieve();
}

interface printer{
   public function printBlack();
   public function printColor();
   public function printDraft();
   public function kick();
}

class printerFax implements fax, printer{
   public function dial(){ }
   public function send(){ }
   public function recieve(){ }
   public function printBlack(){ }
   public function printColor(){ }
   public function printDraft(){ }
   public function kick(){ }
 }

 $object = new printerFax;
 ?>

Further thoughts, to be more specific: what does the interface actually do? I asked if it is a comment - many people imply that it is. I asked if it wasn't DRY.

I have since found this example. What does it mean to "program to an interface"? . I might add it is the only example I have found in hours of searching.

Thanks for your ready responses. If a beginner arrives here, please go to question 383947. The example is not in PHP but it shows the functionality. If anyone knows an example in PHP, I would be grateful.

This still seems undry though. Functions get repeated where they wouldn't in procedural programming.

Community
  • 1
  • 1
jobucks
  • 69
  • 7
  • The key word is that it acts as a *contract*. – Jon Aug 14 '14 at 21:21
  • 1
    An interface is a contract / promise to the developer, that anything implementing it has specific functions that can be called, without needing to either extend a base class or provide default implementations. If you define a method `faxDocument(fax $fax);`, you are saying: "provide anything that has a dial(), send() and receive() function", so you can safely _know_ you can call them. – Wrikken Aug 14 '14 at 21:21
  • All classes extending or implementing the interface MUST define all methods that are defined in the interface. – Ryan Aug 14 '14 at 21:24
  • Thanks for the swift replies Jon, Wrikken and true. But I that is the kind of wording we see everywhere. It doesn't take me forward. I have just seen this which gives me some intuition (for fellow noobes) http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface?rq=1 – jobucks Aug 14 '14 at 21:32
  • 1
    It really does take some time to understand, but once you get it. You find out where it makes sense to use them. – Ryan Aug 14 '14 at 21:37
  • One advantage of interfaces is that when you type-hint in a method, you can accept a type of the interface as well as a specific class type that implements it. So you could have in another class something like `public function setDevice(fax $device)`. This allows you to accept any implementation of that, regardless of whether it is a `printerFax`, a `traditionalFax` or a `scannerFax`. – halfer Aug 14 '14 at 21:37
  • Halfer - way over my head. I'm a noobe. Still struggling with the basic implementation and basic functionality. But thanks. – jobucks Aug 14 '14 at 21:42
  • Can you outline what part of my response you're not sure about, @jobucks? (After posting I realised that I'd said a similar thing to Wrikken - he or she explained it better, IMO). In a nutshell, interfaces are a way of explaining to a language what methods must be offered at a minimum to fulfil its purpose, e.g. to offer fax functions. – halfer Aug 14 '14 at 22:18
  • halfer - the whole thing. Like most noobes who arrive here, I am punch drunk with polysyllabic explantions. I want something that actually shows how this works. I have found one example (one!) that is intuitively clear but it contradicts all the others which are like the one I gave and seem utterly pointless to me. They are lists of functions repeated over and over. Certainly not dry. Can someone please point us to a working example? Thanks for coming back. I was feeling beaten up too. – jobucks Aug 14 '14 at 22:27
  • (Please use @halfer to contact me, otherwise I won't see a message notification from you and it is likely I will miss a message). – halfer Aug 15 '14 at 15:01
  • The answer from Peter on your link is excellent, and I found it useful. There is no value IMO in converting the example to PHP, as it would only be a couple of very minor tweaks. Can you point to your sources that say that interfaces do not do anything, or that they are like comments? Both statements are wrong, but I might be able to describe _why_ they are wrong if I can see them. – halfer Aug 15 '14 at 15:05

1 Answers1

0

From this answer: https://stackoverflow.com/a/219522/1187982

An interface is an abstract class (in languages like Java where there is no multiple inheritance, sometimes there are other restrictions, such as a separate data type) that is intended to be used as a common base to access a number of similarly-behaving objects. Conceptually, there is no requirement for abstractness, but usually, an interface will have at least one abstract method. An interface is a method for your program to communicate with a number of similar classes, each with different semantics but the same general purpose.

An interface provides no functional benefit, but it does provide you with the guarantee that any inheriting classes will at least have some amount of commonality.

Joshua Shearer
  • 1,120
  • 10
  • 23
  • Thanks Joshua - I would ask how that guarantee operates - or if it as I said, functions as a comment. But I have just seen this. What do you think of that example? http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface?rq=1 – jobucks Aug 14 '14 at 21:35