Question

I'm currently working my way through Code Complete and the word "interface" keeps popping up! I'm trying to get my head round what an interface is. Can you define the term? Also what actually makes up a "class interface"?

Was it helpful?

Solution

I think a good way to define an interface is as follows

An interface is a contract specifying a set of methods, fields and properties which will be available on any implementing object

The actual implementation from language to language may have funny little differences but the principle holds.

I considered adding implemented interfaces to the list above but left it off because it seemed to be a bit too language specific. I think it's OK though because the end effect is almost the same. Tacking on more interfaces is just adding more methods, fields and properties to the contract.

OTHER TIPS

In general, an interface is simply "what the class looks like to the rest of the world".

For example, this class in Java:

class MyClass {
  private string data;
  private string Foo() {...}
  public void Bar(string s){...}
}

could be said to have just the Bar function in its interface. Internally it has a few other members, but they're private, so they're not visible to the outside world.

More commonly, interfaces are also specific types in the language, for example in the following, MyClass implements the interface IMyClass:

interface IMyClass {
  public void Bar(string s);
}

class MyClass implements IMyClass {
  private string data;
  private string Foo() {...}
  public void Bar(string s){...}
}

The interface is now expressed in code, so that any time a variable of type IMyClass is expected, an object of type MyClass can be used, because it implements the correct interface.

It's external face to the world. Usually the set of public methods (members) it exposes.

Technically however they would be 2 different things

An interface would be a public contract. e.g.

interface ILogger
{
  void Log(String str);
}

A class would then 'implement' this interface - in a way saying that it provides this functionality

class ConsoleLogger : ILogger
{
  public ConsoleLogger() { ... }
  public void Log(String str)
  {
    Console.WriteLine("Console Logger " + str);
  }
  // private stuff
}

Users of this service / interface do not have to concern themselves with how it is implemented or who is implementing it? By depending on an interface, the actual implementation can be switched at will.

The interface to a class is its "public face" that other classes can see. It separates the the class's implementation from the way it interacts with other classes. That way different implementations can be swapped out and other classes don't need to know anything about what's behind the interface.

An interface can include both data and function members.

Interface is definition of set of methods that class can impelement. It's mostly used for interface polymorphism.

A interface is like a class but not quite. It has similar traits like a class but it is not an interface. An interface has a variables and methods, "just like the class but the methods declared in interface are by default abstract (only method signature, no body)".

http://beginnersbook.com/2013/05/java-interface/

Interfaces have two definitions. It depends in what context the term Interface is used.

  1. A Classes Interface refers to all the implemented public methods of a class.
  2. An Interface as a Type. i.e using the keyword interface to declare an Interface.

    interface Movable{ 
        moveStraight();
        moveLeft();
        moveRight();
        stop();
    }
    

    So now a class, lets say, Car can implement the interface Movable. This is what is know as the contract. If class Car implements interface Movable, Car guarantees to provide implementations for all the methods declared in Movables declaration.

To conclude, generally an Interface is addressed as a type (definition 2) in a language specific programming context. You will find the first definition on fewer occasions usually in a theoretical or design context.

The classical real life example of an interface is the controls a car.

The break, gas, and wheel would be the interface. The engine and other mechenical aspects would be the implementation. These mechanical aspects can change, we wouldn't know because our interface remains the same.

This is the power of an interface, it hides implementation details so we can work on a higher level of abstraction. We can directly use the functionality of the interface usually without worrying about how the code underneath it is implemented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top