[JAVA] Notes on multiple inheritance

What is multiple inheritance in the first place?

What is inheritance?

First, as a premise, inheritance has the following purposes. (Reference: http://equj65.net/tech/java8mixin/)

  1. Implementation inheritance (reuse)
  2. Specification inheritance (explicitly specifies that the inherited class has a method, that is, from the client side, it is guaranteed that a method is always implemented)

The actual inheritance patterns are the following two patterns.

  1. Inherit both specifications and implementations
  2. Inherit only the specifications.

Inheritance of specifications and implementation.kt


open class Person {
    fun greet() { System.out.print("Hello") }
}

class Japanese: Person{ }

fun main(args: Array<String>) {
    val taro = Japanese()
    taro.greet()
}

Inherit only specifications.kt


interface Person {
    fun greet()
}

class Japanese: Person{ 
    override fun greet() { System.out.print("Hello") }
}

fun main(args: Array<String>) {
    val taro = Japanese()
    taro.greet()
}

As mentioned above, if you inherit only the specifications, you inherit the interface. This is because interface cannot contain an implementation. (It seems that you can have an implementation with _Java8, but I will not touch it here _)

What is multiple inheritance?

It literally means inheriting from multiple classes.

What? What's the problem?

There is a phenomenon called the diamond problem. Look at the code below.

diamondProblem (if implementation can be inherited multiple times).kt


interface Person {
    fun greet()
}

open class Japanese: Person {
    override fun greet() { System.out.print("Hello") }
}

class American: Person {
    override fun greet() { System.out.print("Hello") }
}

//If such multiple inheritance is possible
class Half: Japanese(), American { }

fun main(args: Array<String>) {
    val karl = Half()
    karl.greet()
}

What kind of greeting does karl really make? This is actually a compilation error. This is because Java cannot have multiple parent classes.

Why can't I have multiple parent classes? Consider the case where the inherited parent class (Japanese, American) implements the method with the same name as in the above example. The compiler cannot resolve which method is called when the greet method is called from the inheriting class (Half).

Therefore, due to compiler reasons, Java cannot inherit multiple classes (! = Interface).

To achieve multiple inheritance

Inherit interface instead of inheriting implementation class. However, in this case, you have to write the implementation in the inherited class. Therefore, the implementation cannot be reused, and only the specifications can be inherited.

Consideration

Inheritance of specifications seems to be similar to Duck Typing in Ruby. However, I think that inheritance of specifications is superior in the following points.

  1. You can force the class author to implement
  2. Since it is assumed that the client side has an implementation, there is no need to think about extra error handling.
  3. Type check can be performed, so more reliable development can be performed.

Implementation inheritance seems to be similar to Mix-in in Ruby. However, Mix-in is easier to use because it does not complicate the inheritance structure.

reference

http://equj65.net/tech/java8mixin/ https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html https://en.wikipedia.org/wiki/Multiple_inheritance https://en.wikipedia.org/wiki/Mixin

Recommended Posts

Notes on multiple inheritance
Notes on Protocol Buffers
python notes on docker
[Android] Notes on xml
Notes on regular expressions
Use devise on multiple models
[Ruby] Notes on gets method
Inheritance
Inheritance
Notes on signal control in Java
Notes on calling Installer on Android App
Notes on migrating from CircleCI 1.0 to 2.0
Notes on Android (java) thread processing
Notes on Java path and Package
Notes on handling UTF-8 encoded property files
Manipulate multiple models on form objects [De-accepts_nested_attributes_for]
Launch multiple Redmine instances on one server
Notes on operators using Java ~ String type ~
Notes on expand () and collapse () of Expandablerecyclerview
Notes on creating android plugins for Unity
Notes on using FCM with Ruby on Rails
Notes on Java's Stream API and SQL
Notes on JSP extension tags in SpringFrameWork
Open multiple workspaces in Eclipse on Mac