views:

952

answers:

18

In c#, we have interfaces. Where did these come from? They didn't exist in c++.

+3  A: 

They came from java, and they were introduced because java (and C#) do not allow multiple inheritance.

EDIT: I'm receiving some downmods because people using COM interfaces in C++ disagree with the above statement. Regardless, the concept of an interface came from java, C++ COM interfaces were virtual classes, java was the first language to make it a language feature.

END EDIT

For example, in C++, you could have a class named Dog that inherited from Animal and Mammal.

In C#, you would have a base class named Animal, and use an interface (IMammal). The I naming notation is historical from C++ (It was used to indicate an abstract virtual class), and was carried over to java but is more significant in C#, because there is no easy way to tell what is a base class and what is a interface from a C# class declaration:

public class Dog : Animal, IMammal

while in Java it was more obvious:

public class Dog extends Animal implements IMammal

Multiple inheritance is very tricky, so interfaces were derived to simplify it. A C# class can only inherit from one base class, but can implement N amount of interfaces.

In C++, interfaces can be simulated by using pure virtual classes. These require all methods to be overridden polymorphicaly by the inheriting class.

FlySwat
Can you elaborate a bit more?
Cetra
Java got the idea of interfaces from Objective-C: http://cs.gmu.edu/~sean/stuff/java-objc.html
Jason Baker
IDL had interfaces way before Java
Mark Cidade
Java was not the first language to add explicit support for interfaces. They existed as an explicit language keyword as early as 1986, in the language Modula-3 (see my post below for more details).
Scott Wisniewski
A: 

It's just another layer of abstraction. Not really sure where it came from.. I still often hear them called contracts rather than interfaces.

Ian P
They don't abstract anything. They are a strict ruleset that a class must follow.
FlySwat
They abstract what is actually going on. The interface sets up a strict ruleset only on semantic interfaces, no guaratee on what is going on in the implementor.
Statement
+1  A: 

C++ allows for multiple inheritance. When Java was developed, single inheritance was decided upon however classes were allowed to implement multiple interfaces. C# carried forward this concept.

Ben Hoffstein
A: 

I think the basic idea is "multiple inheritance". So the idea came from C++.

A: 

Well there wasn't any language integrated mechanism syntax for it, but you can achieve interfaces in C++ with pure virtual classes.

class IFoo
{
public:
  void Bar() =0;
  void Bar2() =0;
};

class Concrete : public IFoo
{
public:
  void Bar() { ... }
  void Bar2() { ... }
}
Statement
+1  A: 

They existed in C++, but they were known as virtual base classes, which consisted only of pure virtual functions. This is where the "I-" prefix for interfaces came from -- to differentiate between virtual base classes from abstract base classes.

moffdub
Interfaces are more limited than virtual base classes, and for good reason. For example, you cannot apply polymorphism to an interface.
FlySwat
I believe you can extend interfaces in C#. Haven't tried it in Java yet. I don't know if that is the type of polymorphism you're referring to.
moffdub
+1  A: 

i've seen the keyword interface first in java, but they are much older than that.

the same concept exists in c++ but it is not exactly the same. They are called "pure virtual classes"

http://en.wikipedia.org/wiki/Virtual_function

They exists with different syntax but are there to allow polymorphism in OOP.

Eric
A: 

In C++ you could have an abstract class with no implementation, and you could inherit multiple classes. Java and C# got rid of multiple inheritance, so in order to have the ability to inherit multiple contracts (not behaviors), they created interfaces. You can only inherit one class in C#, but you can inherit as many interfaces as you want.

An interface is jst a contract. It says which members that an instance must implement. It does this, however, without implementing any default behaviors.

Charles Graham
+5  A: 

Interfaces were also a central part of COM, which was a very successful technology for separating interfaces from implementation.

17 of 26
COM is not a language. Interfaces are a feature in the language.
Tony Lambert
+2  A: 

Interfaces existed in C++ if you did COM programming, which is where the IPrefix convention originates.

Although C++ itself didn't natively support interfaces, COM/C++ used type libraries generated from Interface Definition Language which has the only purpose of defining interfaces, and used the interface keyword long before Java or C# did.

Aside from allowing a form of multiple inheritence, .NET's motivation for interfaces have to do with its component-oriented origins and its main purpose is to define contracts between components that can interoperate without any knowledge of each other's implementations. Some COM interop is also done with .NET interfaces.

Mark Cidade
Yes, but they were not a language feature.
FlySwat
Technically, interfaces were a language feature. They were defined semantically (create a class under these conditions and the compiler will emit code that corresponds to that binary interface).The fact that the feature is semantic, and not syntactic, doesn't make it less of a feature.
Scott Wisniewski
Also, they were syntactic features of the COM and CORBA interface definition languages, which were languages designed specifically for defining interfaces. In any case neither C++, MSIDL,or CORBA (which predate Java) were the first languages to offer such features. They have been around "forever".
Scott Wisniewski
+1  A: 

The earliest implementation of interfaces that I know of in computing came from CORBA.

My understanding is that the concept came out of electrical and electronic engineering where a power outlet in a wall for instance can be used (and implemented) by anyone who knows the specification. Interfaces then provide the same flexibility programatically.

Incidentally while they were not created to reduce versioning problems they can certainly help with them.

+3  A: 

I was under the impression that the first formalized concept of interfaces came from Objective-C (called "protocols"). I can tell you for sure that Java at least got the idea from Objective-C, so it wasn't Java that had interfaces first.

Email from Patrick Naughton

Jason Baker
It's amazing how wrong the other answers are... I think strictly speaking SmallTalk -> Objective C -> Java.
Tony Lambert
+9  A: 

Interfaces are pretty old, and have been around for quite a while.

Early (mid to late late 1970's) non-object oriented languages such as Modula and Euclid used constructs called "modules" to specify the interfaces between components. Components would then communicate with each other via explicit importing and exporting modules. Interfaces in C# are object oriented evolutions of that same concept.

Interfaces in C# directly extend from the concept of interfaces in C++ (and Java), where they were used as part of COM for describing object-oriented component interfaces.

EDIT: In doing a small amount of research, the earliest language I could find with an explicit "interface" keyword was Modula-3, a derivitive of Modula created around 1986.

Scott Wisniewski
A: 

Interfaces came from computer science. Or, let's say, from common sense in programming. Interface is a logical group of methods of a class. C++ didn't need a separate language concept of "interface", because any class might be used as an interface -- just define a set of methods in it, make no implementation, call it like IExecutable and use:

class IExecutable
{
public:
    virtual void Execute() = 0;
};

class MyClass : public IExecutable
{
public:
    void Execute() { return; };
};

Some languages, called "dynamically typed", like Python, don't require to define interfaces at all, you just call a method you need, and run-time checks if it is possible ("If it walks like a duck and talks like a duck, it must be a duck").

C# clearly separates a concept of interfaces from classes, because it uses static typing... and multiple inheritance is prohibited in that language, but it is ok for a class to have one base class and another interface, or to implement several interfaces at a time.

public interface IPurring
{
    void Purr();
}

public class Cat : Animal, IPurring
{
    public Cat(bool _isAlive)
    {
        isAlive = _isAlive;
    }

    #region IPurring Members

    public void Purr()
    {
        //implement purring
    }

    #endregion
}
Yacoder
A: 

While not called 'interfaces', C data structure pointers with function pointers as elements of the structure implemented the concept of interfaces long before c++ did with virtual base classes IMO.

kenny
+1  A: 

I trink interfaces came from the fact that some programmers got tired of writing the implementation of a method over and over again. How many times can you write:

static string Method(int i)

without thinking there has to be an easier way?

BBetances
A: 

Interfaces were also used in CORBA. Interface Definition Language (IDL) was used to describe interfaces independently of whatever language the object was implemented in. This separated not only interface and implementation, but also interface and language binding.

Brian Rasmussen