tags:

views:

715

answers:

8

Languages such as Java explicitly use the interface keyword to denote interfaces. Having used Java, the concept seems useful enough to me to justify a keyword to enforce the concept.

Of course one can use a pure virtual class and label it as an interface. However, this keyword seems to be so useful and differentiated from a pure virtual class as to be useful. Perhaps it is being included in C++ 0x?

+17  A: 

It's redundant, since interfaces are represented by having any class member be pure virtual (=0).

Crashworks
If *all* class methods are not virtual, it is not an interface.
@casualcoder: correct. And if *all* class methods are virtual, it's effectively an interface. That's exactly correct.
S.Lott
@S. Lott: what are you saying is correct? casualcoder's comment, or this answer? This answer does not describe an interface, the way you and casualcoder are describing one.
Kip
-1, it's not any class member, it's all class members, so the answer's misleading.
orip
+21  A: 

Because C++ allows multiple inheritance, and because an interface is an abstract class which has all of it's members also abstract/virtual, C++ does not need it - a class can simply "extend" multiple other classes, any of which may be purely virtual (abstract).

Java and C#, on the other hand do not permit MI, since the designers of those languages felt that MI creates more problems than it solves. But it is still necessary for an object to "be" many things (the OOP is-a relationship), so interfaces provide a mechanism which allows an object to be many things, without inheriting multiple implementations - keeping the baby, but throwing out the bathwater.

Software Monkey
It is definitelly not *generally accepted* that MI creates more problems than it solves. In fact, many OO languages support MI: CLOS, Eiffel, Python, OCaml, etc...
Nemanja Trifunovic
+1 @Nemanja Trifunovic - also, some of these languages have explained possible problems that might arise when using MI and proposed correct and reliable solutions to them.
Daniel Daranas
Nor does C++ MI cause more problems than it solves when used properly, like many C++ features. C++ was designed to be the Swiss Army chainsaw of languages.
David Thornley
lol a Swiss Army chainsaw indeed :D
Since an interface is roughly the same as a class with all pure abstract methods it would be nice to have interfaces as syntactic sugar. You need to check all methods of a class to find out if they are all abstract and pure.
EricSchaefer
I second EricSchaefer.
@David T: FWIW, you are assuming the most programmers can use MI properly. Like most error-prone language constructs all it takes to cause more problems than they solve is for more programmers to use them badly than those who use them well.
Software Monkey
+3  A: 

Because interface is strictly weaker than MI.

Joshua
That it is a weaker statement is what differentiates it from a class with all pure virtual methods. The compiler can even enforce the interface paradigm if it had a keyword to do so.
It is the philosophy of the C++ compiler to not enforce paradigms.
Joshua
+7  A: 

Adding an "interface" keyword would add complexity to the implementation without adding any truly useful capability; it would duplicate existing functionality. As others have said, it's just a pure virtual class. Java and C# had to have 'interface' to get a piece of what C++ already had. Philosophically, C++ is designed to enable programmers to write good software, not to prevent programmers from writing bad software. In my experience, the hoopla against MI is way overblown. Idiots misused it, like they misuse everything, and instead of blaming the idiots for being idiots, people blamed the tool.

Rob K
+2  A: 

Interface appears in languages which don't have multiple inheritance, to partially cover for that. C++ already has multiple inheritance, so it doesn't need it.

Also, not all languages need to be the same. C++ has its own design and history and has its strong points and its weaknesses, just like Java, C#, and whatever. It wouldn't be useful to try to make all languages equal.

Daniel Daranas
+1  A: 

The early OO features of C++ have long been neglected because it has since moved in a more interesting direction as a multi-paradigm language. The major focus for over a decade now has been templates and their implications, particularly in the standard library. Yes, programs would be more readable with an interface keyword. They would also be easier to maintain if there were override and new modifiers for methods that have the same name as base class methods (a la C#). But these are not interesting problems to modern C++ users, nor to those who contribute to the language design. The OO features are adequate, but not great, and are hardly used in the "newer" (post 1992) parts of the standard library, which in some ways serves as a guide to good style.

Daniel Earwicker
A: 

Here is a short article from DDJ that touches on the distinction between a class and an interface.

A: 

Actually the "interface" OO concept is the operations that an object could perform.

So languages such as Ruby or Python also have "interfaces" although they don't need to declare them as in Java.

In Java, the interface OO concept is matched with this "interface" keyword used to declare that an object will respond to a certain contract ( a number of methods )

C++ has the concept too, and even every Java object that doesn't implement any interface.

OscarRyz