Multi-inheritance in Kotlin? (Based on Protocol Oriented Programming šŸŽ)

Brayan Armando Yaquian Gonzalez
4 min readJun 6, 2021

Inheritance is the procedure in which one class inherits the attributes and methods of another class.

Hi folks, thanks for being here to discuss a bit about the well-known use case in Android, multi-inheritance. As you might know, it is not possible in languages such as Java (Kotlin in consequence) or Swift.

I want you to keep in mind the definition of inheritance, please note that the child class inherits methods and attributes (variables inside the class) will discuss this later.

Protocol Oriented Programming ā€” iOS way

Protocol

So what iOS folks do to resolve multi-inheritance? the answer is protocol-oriented programming. But wait, what is a protocol? (you probably donā€™t know because youā€™re Android Developer like me šŸ˜…). You can simply think of it as an interface in Java or Kotlin. Nevertheless, they have some differences that weā€™ll discuss later, for simplicity letā€™s call them both contracts.

Although Kotlin and Swift donā€™t support multi-inheritance they allow you to implement multiple contracts in the same class so you might know where this is going on.

Now that you have an idea about what I mean by contract, letā€™s raise a scenario.

Multi inheritance

So letā€™s say that we need to inherit the behavior of Cartoonand Animal into AnimalCartoon. How this can be accomplished in iOS? We already know Cartoonand Animal cannot be Classes since multi-inheritance is not allowed in Swift. So we need to recur to protocols and extensions.

Extension

We already know what a protocol is, but what about extensions?

Extensions add new functionality to an existing class, structure, enumeration, or protocol type.

For instance, you could add a new method for all the Strings of your program with an extension.

Letā€™s not dive into details about this code. What I want you to notice is that the methodcapitalize defined inside the extension is available for all the strings, in this case str.

POP intuition

We have the necessary concepts to try our first POP (Protocol Oriented Programming). The main idea behind this is simulating inheritance from classes using protocols, based on the definition we need a procedure in which one class inherits the attributes and methods of another class.

Protocols in swift

Key concepts about these protocols that we just defined:

  • Protocols in swift preserves the state. That means each class that uses the protocol is forced to declare and initialize the attributes. ā€” tvShowName, isMainCharacter, isMammal, age āœ…
  • Methods declared in protocols canā€™t have bodies. ā€” isVeteran() and printInfo().āŒ

We figured out that a protocol already solves for us the attributes part, but how can we inherit methods if we canā€™t define a body inside the protocol? What a problem! šŸ˜•

Well, we already know the tool to provide new functionality to a protocol, extensions.

Yeah, we did it, now we have a default implementation for printInfo and isVeteran methods! šŸ„³šŸ„³šŸ„³šŸ„³šŸ„³šŸ„³šŸ„³

Now here is the magic! Letā€™s see how our ā€œmulti inheritanceā€ works

Letā€™s how it works properly.

The state is preserved and the default methods are working properly for each new instance mickeyMouse and nemo. Note thatprintInfo method comes from Cartoon extension and isVeteran comes from Animal extension. I didnā€™t have to implement those in AnimalCartoon class.

POP ā€” Kotlin way!

So far weā€™ve seen that the key tools for POP are:

  • Contracts that preserve the state (Disclaimer: it doesnā€™t mean the interface has a state, it just force you to override the variables specified in the contract)
  • Extensions

Do we have that in Kotlin? Of course we do, Actually, we wonā€™t need extensions here because we can implement the methods inside the interfaces šŸ¤Æ

Letā€™s see the same example Animal, Cartoon and AnimalCartoon.

Note how you isVeteran and printInfo are implemented inside the interface. And thatā€™s it, we can ā€œmulti inheritanceā€ now.

Letā€™s see that it works exactly in the same as we saw in swift.

Bouns ā€” Java

Is all this possible in Java? Letā€™s try it out!

Even though we have default methods we have a problem here. When you define a variable inside an interface in Java it is declared as static and final (like a constant) so you are forced to initialize it inside the interface, Unfortunately, that means you cannot preserve the state with a Java interface. šŸ˜­

Disclaimer: It doesnā€™t mean that in java you cannot achieve the same results with another approach like composition or simulating your attributes with get methods.

Goodbye

I hope you enjoyed this little journey along multi inheritance and you learned something new today. See you around!

--

--

Brayan Armando Yaquian Gonzalez

Bachelor in Computer Science, Android Developer at Paypal, Android Associated Developer by Google, Java Programmer 8 Certification.