29

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

4 Answers 4

30

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++:

Sign up to request clarification or add additional context in comments.

1 Comment

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.
2

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.

3 Comments

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.
Corrected 1) to be more precise. My definition is supported by exforsys.com/tutorials/csharp/inheritance-in-csharp.html
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.
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.