DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Top 10 Engineering KPIs Technical Leaders Should Know
  • What Is mTLS? How To Implement It With Istio
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Automating the Migration From JS to TS for the ZK Framework

Trending

  • Top 10 Engineering KPIs Technical Leaders Should Know
  • What Is mTLS? How To Implement It With Istio
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Automating the Migration From JS to TS for the ZK Framework
  1. DZone
  2. Coding
  3. Languages
  4. Kotlin Tutorial: Implementing Multiple Interfaces Through Delegation

Kotlin Tutorial: Implementing Multiple Interfaces Through Delegation

Learn more about implementing multiple interfaces with Kotlin.

Dan Newton user avatar by
Dan Newton
·
Nov. 22, 19 · Tutorial
Like (6)
Save
Tweet
Share
49.04K Views

Join the DZone community and get the full member experience.

Join For Free

Computer with code on screen

Learn more about implementing multiple interfaces with Kotlin.

In Kotlin, a class can implement multiple interfaces. This is common knowledge. A class can also use delegation to implement numerous interfaces where the implementations come from any delegated objects passed into the constructor.

You may also like: Scala Vs. Kotlin: Multiple Inheritance and the Diamond Problem

For example:

class GeneticExperiment(human: Human, animal: Animal) : Human by human, Animal by animal

interface Human {
  fun eat()
  fun sleep()
  fun poop()
}

interface Animal {
  fun bite()
}


This weird class inherits functionality from both the Human and Animal  objects passed into the constructor. Without delegation, any functions on either of the interfaces will need to be written out manually within the class.

An important point to note is the fact that Human  and Animal do not share a common parent interface.

If you do have interfaces that extend from a common interface, then there will be compilation errors due to conflicting implementations from the delegated objects.

For example, the code:

class ShapeShifter(human: Human, weightLifter: WeightLifter) : Human by human, WeightLifter by weightLifter

interface Human {
  fun eat()
  fun sleep()
  fun poop()
}

interface WeightLifter : Human {
  fun liftHeavyStuff()
  fun pose()
}


Leads to the errors:

Delegation.kt:80:1: error: class 'ShapeShifter' must override public open fun eat(): 
Unit defined in dev.lankydan.ShapeShifter because it inherits many implementations of it
class ShapeShifter(human: Human, weightLifter: WeightLifter) : Human by human, WeightLifter by weightLifter
^
Delegation.kt:80:1: error: class 'ShapeShifter' must override public open fun sleep(): 
Unit defined in dev.lankydan.ShapeShifter because it inherits many implementations of it
class ShapeShifter(human: Human, weightLifter: WeightLifter) : Human by human, WeightLifter by weightLifter
^
Delegation.kt:80:1: error: class 'ShapeShifter' must override public open fun poop(): 
Unit defined in dev.lankydan.ShapeShifter because it inherits many implementations of it
class ShapeShifter(human: Human, weightLifter: WeightLifter) : Human by human, WeightLifter by weightLifter
^


As you can see in the errors, the ShapeShifter  class does not know which delegated implementation to take. The compilation errors must be resolved by explicitly overriding the conflicting functions. By overriding the functions, you can decide which delegated implementation you wish to use, or you can provide a new one altogether. A fixed version of this class could look like:

class ShapeShifter(
  private val human: Human,
  private val weightLifter: WeightLifter
) : Human by human, WeightLifter by weightLifter {

  override fun eat() {
    human.eat()
  }

  override fun sleep() {
    weightLifter.sleep()
  }

  override fun poop() {
    println("��")
  }
}

interface Human {
  fun eat()
  fun sleep()
  fun poop()
}

interface WeightLifter : Human {
  fun liftHeavyStuff()
  fun pose()
}


Only the functions declared in the Human interface need to manually overridden

Taking a slight step back from here. A class that implements a child interface and delegates its functionality to the child’s parent interface will compile as long as all of the child’s functions are implemented. Wording that was difficult. An example should help clarify what I am trying to say:

class ShapeShifter(copied: Human) : WeightLifter, Human by copied {

  override fun liftHeavyStuff() {
    println("��️♂️")
  }

  override fun pose() {
    println("��")
  }
}

interface Human {
  fun eat()
  fun sleep()
  fun poop()
}

interface WeightLifter : Human {
  fun liftHeavyStuff()
  fun pose()
}


Only the functions declared in the WeightLifter  interface need to be added manually

To summarize:

  • A class can implement multiple interfaces and delegate its functionality to one or more objects
  • A class with multiple interfaces that extend a common parent can delegate each interfaces’ implementation but must override functions defined in the parent
  • A class that implements a child interface can delegate to its parent and only needs to add functions defined in the child.

If you enjoyed this post or found it helpful (or both), then please feel free to follow me on Twitter at @LankyDanDev and remember to share with anyone else who might find this useful!

Further Reading

Class Delegation in Kotlin

Scala Vs. Kotlin: Multiple Inheritance and the Diamond Problem

Interface (computing) Kotlin (programming language) Delegation (object-oriented programming)

Published at DZone with permission of Dan Newton, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Top 10 Engineering KPIs Technical Leaders Should Know
  • What Is mTLS? How To Implement It With Istio
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Automating the Migration From JS to TS for the ZK Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: