-6

I am confused over Virtual Inheritance and disinheritance ? Is both are same ?

closed as unclear what you're asking by gnat, manlio, Robert Harvey, David Arno, user22815 Jun 27 '16 at 22:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    There's no such thing as "disinheritance." This patent seems to have coined the term to describe a form of "monkey patching," but that appears to be the sole significant reference. – Robert Harvey Jun 26 '16 at 19:23
  • 6
    Where did you see the term "disinheritance"? I've not previously come across that terminology. – Kevin Jun 26 '16 at 19:24
2

Virtual inheritance

Virtual inheritance is a technique to solve the problem of the diamond of death that can arise when via multiple inheritance you can inherit the same base class several times.

For example, suppose you have a class Person, and two derived classes of it: Student and Employee. Now you can have a class StudentWorker that inherits from Student AND from Employee. So this class inherits twice the class Person. But in reality there should be only one Person, with a unique name and date of birth, etc... You can achieve this with virtual inheritance.

class Person {...};  
class Student: public virtual Person {...}; 
class Employee: public virtual Person {...}; 
class StudentWorker: public Student, public Employee {...}; 

Disinheritance

The idea of disinheritance is to get rid of some inherited member. The problem is that disinheritance would infringe the Liskov substitution principle and break core assumptions about derived objects. This is certainly why it is not supported by modern mainstream OOP languages as far as I know (for example Java). (there seems however to exist a patent for a runtime implementation of the concept)

There are several strategies that can help to achieve the underlying need:

  • A usual workaround is to override the function to be desinherited, and trigger an exception in case it is called.
  • Another approach is to decompose what is inherited and (over)use multiple inheritance in combination with a mixin architecture

Some will claim that the need for disinheritance is the symptom of a design flaw. Alternatives to be considered:

  • private inheritance, to hide the inheritance downstream the inheritance hierarchy.
  • composition, using some proxy members to address the composed object frequently does address such (des)inheritance problems, especially when the principle of composition over inheritance is not rigorously enforced.

Similarities

So in general, and in absence of an authoritative definition of the term, disinheritance has nothing to do with virtual inheritance.

However, the virtual inheritance can somehow be analyzed as a way of getting rid of multiple redundant base classes. So I can imagine that in this very specific context, some people could consider it as a kind of disinheritance. Personally, I think this terminology would be abusive here, because it's not so about getting rid of the inheritance, but "factorizing" (in the mathematical sense) redundant base elements into a common base element.

  • 1
    is there a source that one can check for what "disinheritance" means? or is this the way how you personally interpret this term? – gnat Jun 26 '16 at 20:25
  • 1
    @gnat There are some questions around on SO in relation with C++, CSS, and other languages. It's a term which seems to be rooted in the Smalltalk terminology. It's used in articles, blogs and books for other languages as well ([F# example]( trelford.com/blog/post/Disinherited.aspx)). So I didn't find a single authoritative definition of it, but all these sources are consistent on the use of this terms, so I build on it. Of course, if OP gives is definition, I could fine tune on more specific meaning. – Christophe Jun 26 '16 at 20:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.