Multi-inheritance in Kotlin? (Based on Protocol Oriented Programming š)
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.
So letās say that we need to inherit the behavior of Cartoon
and Animal
into AnimalCartoon
. How this can be accomplished in iOS? We already know Cartoon
and 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.
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()
andprintInfo().
ā
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!