2

A 2013 study of 93 open source Java programs (of varying size) found that

While there is [no] huge opportunity to replace inheritance with composition (...), the opportunity is significant (median of 2% of uses [of inheritance] are only internal reuse, and a further 22% are only external or internal reuse). Our results suggest there is no need for concern regarding abuse of inheritance (at least in open-source Java software), but they do highlight the question regarding use of composition versus inheritance. If there are significant costs associated with using inheritance when composition could be used, then our results suggest there is some cause for concern. — Tempero et al., "What programmers do with inheritance in Java"[4]

From : https://en.wikipedia.org/wiki/Composition_over_inheritance#Empirical_studies_of_composition_and_inheritance

What is the meaning of reuse of inheritance?

q126y
  • 1,713
  • 3
  • 14
  • 24
  • [Code reuse via inheritance](http://programmers.stackexchange.com/questions/187876/code-reuse-via-inheritance) – gnat Dec 11 '15 at 12:07
  • Every case of inheritance can be replaced with composition by use of a Pimpl (**p**ointer to **impl**ementation) style delegation instead of overridden methods. – ratchet freak Dec 11 '15 at 12:18
  • @gnat Then, how is other 98% or 78% of inheritance is used as mentioned in the question? As I understand, all inheritance is code reuse, except for interface implementation, which in Java, unlike C++ has different keyword, and is definitely not the case here. – q126y Dec 11 '15 at 12:21
  • 1
    @q126y did you read the pdf the quote is from, https://www.cs.auckland.ac.nz/~ewan/qualitas/studies/inheritance/TemperoYangNobleECOOP2013-pre.pdf – ratchet freak Dec 11 '15 at 13:03
  • They don't talk about reuse of inheritance; they simply talk about reuse. I'd say it is common to refer inheritance as reuse of a base class or reuse of code (in a base class). – Erik Eidt Dec 11 '15 at 17:06
  • I find it amusing that gnat's link is to a question that was closed as an exact duplicate, which points to a question that was also closed as an exact duplicate. – Matthew James Briggs Dec 12 '15 at 01:39

2 Answers2

2

My guess is that by "reuse of inheritance" they mean code reuse by means of inheritance, as opposed to code reuse by means of composition. The are reusing code, not reusing inheritance. By internal reuse my guess is that they are talking about extending classes in the same package, whereas external is about extending classes from different packages.

Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
  • Then, how is other 98% or 78% of inheritance is used as mentioned in the question? As I understand, all inheritance is code reuse, except for interface implementation, which in Java, unlike C++ has different keyword, and is definitely not the case here. – q126y Dec 11 '15 at 12:58
  • The text doesn't mention anything about the other 98% or 78%. – Tulains Córdova Dec 11 '15 at 13:08
  • what i meant was, is there any other use of inheritance apart from code reuse? – q126y Dec 11 '15 at 13:09
  • 1
    You can use inheritance to leverage polymorphism without code reuse (extending from 100% abstract classes ). – Tulains Córdova Dec 11 '15 at 13:11
  • Or, never call `super` in any of your overriding methods and never call other methods of the same class. – Jörg W Mittag Dec 11 '15 at 13:17
  • I think they *don't actually use* the phrase _reuse of inheritance_ anywhere. They only say _use of inheritance_ or _reuse_ (as the implied context of using inheritance). That said, I agree with your answer. – toniedzwiedz Dec 11 '15 at 14:24
1

It is a bit unclear, but my educated guess (because no other hypothesis seems to make sense) is that:

  • By "inheritance for external [code] reuse" they mean cases where a subsystem exposes (makes publicly available) an abstract base class, which is to be extended by other classes either of this subsystem or of other subsystems. So, in essence, code that uses the base class has knowledge of the fact that inheritance is utilized.

Example:

public abstract class A {};
/* package-private or public */ class B extends A {};
/* package-private or public */ class C extends A {};
  • By "inheritance for internal [code] reuse" they mean cases where a subsystem exposes (makes publicly available) an interface, which is internally implemented by a number of classes that are not publicly exposed, and in order to achieve code reuse the subsystem makes use of an also package-private common base class for all the implementing classes. But none of these classes are publicly exposed, so code outside of the package has no knowledge of the fact that the implementations of the interface are extending some common base class.

Example:

public interface I {};
/* package-private */ abstract class A implements I {};
/* package-private */ class B extends A {};
/* package-private */ class C extends A {};

Of course, internal reuse is best, because it allows for maximum flexibility: the subsystem is hiding from the outside world the fact that it is internally using inheritance, so the subsystem can later be refactored to use composition instead of inheritance, and the outside world will not take notice. (Outside code will not even have to be recompiled.) In essence, when inheritance is employed for internal code reuse, the common base class is nothing but a helper.

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111