2

as I found, an abstract class is an interface when it has zero implementation in it. am I right ?

then why there are interface classes in some languages, I mean C++ doesnt have an interface.

is it going to be obsolete someday soon ?

Arrabi
  • 3,718
  • 4
  • 26
  • 38

6 Answers6

6

a single class can be inherited from only one class. however multiple interfaces can be applied on a single class. abstract classes cannot replace interfaces.

neebz
  • 11,465
  • 7
  • 47
  • 64
  • and why in c# they divided them ? why not allowing multiple inheritance in c# abstract and thats it. – Arrabi May 01 '11 at 15:02
  • 1
    Multiple Inheritance raises confusion for strongly typed languages. If A inherits from X and Y. And if X and Y both have a method with the same name for example foo(). Now when A calls foo, how would the compiler know which foo to call? – neebz May 01 '11 at 15:07
4

C++ does not have interfaces because it supports multiple inheritance, which C# does not. C# provides interfaces to get around this hurdle, so no, interface will not become obsolete.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • C++ does have interfaces. An abstract class is an interface. – David Heffernan May 01 '11 at 14:52
  • You may call it that because it behaves like one, but interface is not the word for it. They serve the same purpose. – Seth Carnegie May 01 '11 at 14:54
  • 2
    Bjarne Stroustrup describes them as so. Design and Evolution of C++, section 13.2, "An abstract class represents an interface." – David Heffernan May 01 '11 at 14:57
  • The point of my answer was that C++ doesn't have a separate `interface` construct. You just make inheritance and classes work together in a certain way and it becomes one. And the quote from DEC sounds more like "C++ version of interface is the abstract class." – Seth Carnegie May 01 '11 at 14:58
  • thats my point, so why they just give the abstract (c#) some more flexibility, so it matches the c++ abstract ? – Arrabi May 01 '11 at 14:59
  • 1
    @Arrabi because multiple inheritance has a lot of quirks and ins and outs that make it somewhat complicated. Single inheritance is far simpler, so they decided to stick with that and provide interfaces when you need to "inherit" from multiple things. My understanding is that Anders didn't want people to have to deal with multiple inheritance, or add complication to the language to support it. – Seth Carnegie May 01 '11 at 15:00
2

Interface are present in language such as Java in order to supply to the lack of multiple inheritance. For this reason the language provide the possibility of inheritance from just one base class, but guarantee the possibility to implement different interfaces.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
1

No, because interfaces have a property that abstract classes with only abstract methods (or any other kind of class) do not: a class may implement arbitrarily many interfaces, while a class may only inherit from one other class. So if you take all of your interfaces and turn them into abstract classes, you will never be able to make a class "implement" more than one "interface".

It can be noted, however, that your observation is correct in certain other languages than C#. For instance, in C++, there is no separate interface construct; "interfaces" in C++ are abstract classes with only abstract methods.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
1

C++ doesn't have special syntax for an interface, e.g. an interface keyword because that just happens to be the design decision taken by the language designer. But C++ does indeed offer full support for interfaces.

Interfaces are not going to become obsolete. In fact the technique of using interfaces is becoming more and more widely used.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

C++ does not have interfaces because unlike Java it allows multiple inheritance. Which in a way complicates things.

Andro
  • 2,232
  • 1
  • 27
  • 40
  • and why in c# they divided them ? why not allowing multiple inheritance in c# abstract and thats it – Arrabi May 01 '11 at 15:00
  • Well I think that is because Microsoft wanted to create a simple useful, clean language. And multiple inheriance complicates things. But interfaces keep things more clear. It is always clear which interface you are implementing. But with multiple inheritance things that seem to be equal to one another are actually very different. – Andro May 01 '11 at 15:08
  • I know that this question has been resolved for a long time, but just today I was thinking about it and I have yet another reason to justify the C# interfaces. It is the way hat .Net enables usage of COM. – Andro Nov 08 '11 at 21:12