2

I understand that Java does not have multiple inheritance.

I'm reading Effective Java 3rd Edition Item 20 (page 102), here is a portion from the page:

The beauty of skeletal implementation classes is that they provide all of the implementation assistance of abstract classes without imposing the severe constraints that abstract classes impose when they serve as type definitions. For most implementors of an interface with a skeletal implementation class, extending this class is the obvious choice, but it is strictly optional. If a class cannot be made to extend the skeletal implementation, the class can always implement the interface directly. The class still benefits from any default methods present on the interface itself. Furthermore, the skeletal implementation can still aid the implementor’s task. The class implementing the interface can forward invocations of interface methods to a contained instance of a private inner class that extends the skeletal implementation. This technique, known as simulated multiple inheritance, is closely related to the wrapper class idiom discussed in Item 18. It provides many of the benefits of multiple inheritance, while avoiding the pitfalls.

Since English is not my native language, the bold part of the quote got me confused. And I got no result when search for the answer.

What is this private inner class? Is this like a normal inner class?

In practice, does this technique get use very often?

Any code example would be extremely useful for me to visualize!

Thank you!

ABC
  • 107
  • 1
  • 7
  • Take a look [here](https://stackoverflow.com/questions/13436995/why-and-when-to-use-skeletal-implementation-in-java). It may not answer your question, but is useful I guess. – Zakk Mar 19 '22 at 19:19
  • @Zakk Thank you. But I already read it before I ask the question. Unfortunately, without an example, I still cannot visualize it correctly. – ABC Mar 19 '22 at 19:23
  • _"Is this like a normal inner class?"_ Yes, it is. I don't know if [this](https://stackoverflow.com/a/6264730/16835308) answers your question. – Zakk Mar 19 '22 at 19:45
  • @Zakk Thank you. I think I found the answer from [this useful article](https://10kloc.wordpress.com/2012/12/03/abstract-interfaces-the-mystery-revealed/). It has a nice example but never mentions **simulated multiple inheritance** but I guess that's acceptable. – ABC Mar 19 '22 at 19:57
  • So, please answer your own question and mark it as the accepted one. This way, others can benefit from it. – Zakk Mar 19 '22 at 20:00

1 Answers1

1

What is this private inner class?

An example:

class OuterClass {
    private class InnerClass {
        {
            
        }
    }
}
StepUp
  • 30,747
  • 12
  • 76
  • 133
  • I understand the `private inner class` now but my main question is about `simulated multiple inheritance`. Any concrete code example? Thanks! – ABC Mar 22 '22 at 20:36