27

I found those two terms in the book of Meyers, but what is the difference?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
helloStack
  • 295
  • 1
  • 3
  • 4

4 Answers4

28

Interface inheritance is public inheritance, while implementation inheritance is private inheritance.

If class B publicly inherits from A, B is an A: it inherits the whole interface of A, and a (reference/pointer to) a B object can be automatically be upcasted to A, and used wherever an object of A is expected. However, if B privately inherits from A, B is-implemented-in-terms-of A: only the implementation of A is inherited, not its interface. Thus (references/pointers to) B objects can not be used in places where A objects are expected.

Update

To reflect on @Michal's comment, here are some links (based largely on googling "c++ implementation inheritance") to demonstrate the common usage of these terms in the context of C++:

Community
  • 1
  • 1
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 1
    Note that `private` inheritance should only be used in a very restricted set of cases (mainly: virtual override / Empty Base Optimization). "Implementated in terms of" relationship is best implemented by composition. – Matthieu M. Sep 23 '10 at 08:22
1

The major difference is interface is public inheritance and implementation is private inheritance. The data members and method of the public and protected section will be inherited from base class to derived class in their respective access specifier in public inheritance.At the same time the object of derived class can access the data members of base class as the normal method. The data members and methods of public and protected section will be inherited from base class to derived class in private access specifier

Redmen Ishab
  • 2,199
  • 18
  • 22
1

Implementation (or class) inheritance is when you separate a common part of implementation in the base class.

Interface inheritance is when you use virtual methods. It is intended to separate interface from implementation and minimize dependencies between program elements.

Michal Czardybon
  • 2,795
  • 4
  • 28
  • 40
  • 1
    Your statements are incorrect on several counts: 1) you don't need to add any new fields/methods in the derived class in order to inherit its implementation, 2) you need no virtual methods for inheriting the interface of a class, it happens by default in case of public inheritance. – Péter Török Jan 12 '11 at 09:33
  • Corrected 1) to be more precise. My definition is supported by http://www.exforsys.com/tutorials/csharp/inheritance-in-csharp.html – Michal Czardybon Jan 12 '11 at 09:50
  • note again that this is C++, not C# :-) C++ as a language does not even contain the concept of *interface* - in practice it is implemented as a class containing only pure virtual methods. – Péter Török Jan 12 '11 at 10:02
0

Here's the difference between the two types of inheritance according to "Taligent's Guide to Designing Programs".

Inheritance

There are two forms of inheritance in C++: type inheritance and implementation inheritance. In both forms of inheritance, a derived class can share or override behavior inherited from a base class. However, use type inheritance only when it is necessary for a derived class to inherit type information as well. The primary reason to inherit type information is to allow for polymorphism.

Express type inheritance by deriving a class from a public base class; express implementation inheritance by deriving a class from a private or protected base class.

More at: https://root.cern/TaligentDocs/TaligentOnline/DocumentRoot/1.0/Docs/books/WM/WM_23.html