therealhoff
therealhoff

Reputation: 2375

Where did the concept of Interfaces come from?

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

Upvotes: 11

Views: 2150

Answers (18)

Brian Rasmussen
Brian Rasmussen

Reputation: 116471

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.

Upvotes: 0

BBetances
BBetances

Reputation: 925

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?

Upvotes: 1

kenny
kenny

Reputation: 22404

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.

Upvotes: 0

Massimiliano
Massimiliano

Reputation: 17000

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
}

Upvotes: 0

Scott Wisniewski
Scott Wisniewski

Reputation: 25061

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.

Upvotes: 12

Mark Cidade
Mark Cidade

Reputation: 100027

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.

Upvotes: 2

Statement
Statement

Reputation: 4068

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() { ... }
}

Upvotes: 0

Jason Baker
Jason Baker

Reputation: 198777

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

Upvotes: 3

who
who

Reputation:

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.

Upvotes: 1

FlySwat
FlySwat

Reputation: 175713

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.

Upvotes: 2

17 of 26
17 of 26

Reputation: 27382

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

Upvotes: 5

user17000
user17000

Reputation: 9

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

Upvotes: -6

Charles Graham
Charles Graham

Reputation: 24845

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.

Upvotes: 0

Eric
Eric

Reputation: 19873

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.

Upvotes: 1

moffdub
moffdub

Reputation: 5314

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.

Upvotes: 1

Ben Hoffstein
Ben Hoffstein

Reputation: 103385

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.

Upvotes: 1

Ian P
Ian P

Reputation: 12993

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

Upvotes: -5

Related Questions