Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between interface inheritance and implementation inheritance

Tags:

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

like image 810
helloStack Avatar asked Sep 22 '10 22:09

helloStack


People also ask

What is the difference between implementation and interface inheritance?

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.

What is the main difference between interface and implementation?

a Interface declares the methods and fields and if a class implements that interace it must have all methods and fields of that interface. interface is the one which contains oly the abstract methods, and the class which contains methods which gives the definition to those abstract methods are implementation.

What is implementation inheritance?

Implementation inheritance is the mechanism whereby a subclass re-uses code in a base class. By default the subclass retains all of the operations of the base class, but the subclass may override some or all operations, replacing the base-class implementation with its own.

Is implementing interface inheritance?

Interface inheritance and interface implementation are not the same thing. A class implements an interface by declaring that it implements an interface and then containing the required members to to implement that interface. Interface inheritance refers to an interface inheriting from one or more other interfaces.


3 Answers

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
like image 122
Péter Török Avatar answered Oct 07 '22 18:10

Péter Török


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.

like image 22
Michal Czardybon Avatar answered Oct 07 '22 19:10

Michal Czardybon


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

like image 38
ishab acharya Avatar answered Oct 07 '22 19:10

ishab acharya