开发者

difference between interface inheritance and implementation inheritance

开发者 https://www.devze.com 2023-01-17 01:05 出处:网络
I found those two terms in the book of Meyers, but what is the differ开发者_Python百科ence?Interface inheritance is public inheritance, while implementation inheritance is private inheritance.

I found those two terms in the book of Meyers, but what is the differ开发者_Python百科ence?


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

  • C++ Design/Coding tips - Part 7
  • Interfaces
  • Uses and Abuses of Inheritance, Part 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号