Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Companion Objects in Kotlin Interfaces

I am trying to make a interface Parcelable, as such I need a interface like this

interface AB : Parcelable {     companion object {         val CREATOR : Parcelable.Creator<AB>     } } 

and my two classes A and B looking like

data class A (...): Parcelable{     ...     companion object {         val CREATOR : Parcelable.Creator<AB> = object : Parcelable.Creator<AB> {             override fun newArray(size: Int): Array<AB?> {                 return arrayOfNulls(size)             }              override fun createFromParcel(parcel: Parcel): AB {                 return A(parcel)             }          }     } 

I am struggling to implement such a interface in kotlin. It seems the interface class does not allow for the CREATOR

Perhaps I am taking the wrong approach,
I have a parcelable that contains a list of classes that are either A or B so I am doing

parcel.readTypedList(this.list, AB.CREATOR) 

I require that the list be either A or B and that is why I am using an interface.

Anyone have any advice or a possible solution?

like image 896
mickstar Avatar asked Feb 10 '16 22:02

mickstar


People also ask

Can an interface have a companion object Kotlin?

Much like Ruby's mixins, Kotlin also bypasses the need for crazy inheritance chains and allows us a more flexible way to define behaviours on our classes by allowing interfaces to not just enforce, but also define the methods for our classes to use.

What is a Kotlin companion object?

Answer: Companion object is not a Singleton object or Pattern in Kotlin – It's primarily used to define class level variables and methods called static variables. This is common across all instances of the class.

What is object and companion object in Kotlin?

Object expressions are executed (and initialized) immediately, where they are used. Object declarations are initialized lazily, when accessed for the first time. A companion object is initialized when the corresponding class is loaded (resolved) that matches the semantics of a Java static initializer.

How do you make a companion object class in Kotlin?

To create a companion object, you need to add the companion keyword in front of the object declaration. The output of the above code is “ You are calling me :) ” This is all about the companion object in Kotlin. Hope you liked the blog and will use the concept of companion in your Android application.


2 Answers

In Kotlin, an interface can have a companion object but it is not part of the contract that must be implemented by classes that implement the interface. It is just an object associated to the interface that has one singleton instance. So it is a place you can store things, but doesn't mean anything to the implementation class.

You can however, have an interface that is implemented by a companion object of a class. Maybe you want something more like this:

interface Behavior {    fun makeName(): String }  data class MyData(val data: String) {     companion object: Behavior {  // interface used here         override fun makeName(): String = "Fred"     } } 

Note that the data class does not implement the interface, but its companion object does.

A companion object on an interface would be useful for storing constants or helper functions related to the interface, such as:

interface Redirector {     fun redirectView(newView: String, redirectCode: Int)      companion object {         val REDIRECT_WITH_FOCUS = 5         val REDIRECT_SILENT = 1     } }  // which then can be accessed as: val code = Redirector.REDIRECT_WITH_FOCUS 
like image 57
2 revs Avatar answered Sep 19 '22 11:09

2 revs


By convention classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.

You need to annotate CREATOR property with @JvmField annotation to expose it as a public static field in containing data class.

Also you can take a look at https://github.com/grandstaish/paperparcel — an annotation processor that automatically generates type-safe Parcelable wrappers for Kotlin and Java.

like image 43
Ilya Avatar answered Sep 18 '22 11:09

Ilya