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,733

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,570
  • 13
  • 100
  • 156
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,803