0

I have the following code -

package multipleInterfaceDemo

fun main() {
    println(MyClass(val1 = 1, val2 = 2).myFun())
}

private class MyClass(override val val1: Int, override val val2: Int): MyInterface1, MyInterface2 {
    /*override fun myFun() {
        super<MyInterface1>.myFun()
    }*/

    override fun myFun() {
        super<MyInterface2>.myFun()
    }
}

private interface MyInterface1 {
    val val1: Int
    public fun myFun() {
        println(val1)
    }
}

private interface MyInterface2 {
    val val2: Int
    public fun myFun() {
        println(val2)
    }
}

Here I have two private Interfaces - MyInterface1 and MyInterface2

Each interface has an Int type variable - val1 and val2 respectively which are set through constructors in implementing Class

Both my Interfaces have a method called myFun() which prints out val1 and val2 respectively.

Now I have a class MyClass that implements MyInterface1 and MyInterface2.

The Class has two constructor parameters for setting the variable values in the two interfaces implemented by the Class

Now both the Interfaces have a method having similar name - myFun() So there is ambiguity regarding method of which Interface is being implemented by overriding.

Here I clear the ambiguity by calling super method myFun() by using super keyword and after super placing angular brackets and within the brackets mentioning the super Interface type - MyInterface1 or MyInterface2

Now the problem which arises here is that I can override either the myFun() method of Interface1 or of Interface2. But I can't call the myFun() method of both the Interfaces at the same time.

So is it possible to do any code tweak so that I can call myFun() method of both Interface1 and Interface2 at the same time?

A similar C# question already exists -

Inheritance from multiple interfaces with the same method name

But I am unable to implement the answers in Kotlin

Payel Senapati
  • 1,134
  • 1
  • 11
  • 27

2 Answers2

3

Not quite sure that this is what you need, but you can use both super<MyInterface1>.myFun() and super<MyInterface2>.myFun() in the same myFun function

private class MyClass(override val val1: Int, override val val2: Int): MyInterface1, MyInterface2 {
    override fun myFun() {
        super<MyInterface1>.myFun()
        super<MyInterface2>.myFun()
    }
}
IR42
  • 8,587
  • 2
  • 23
  • 34
0

Answer provided by IR42 is good but the following approach suits me better -

class MyClass() {
    class MyInnerClass1(override val val1: Int): MyInterface1 {
        override fun myFun() {
            super<MyInterface1>.myFun()
        }
    }
    class MyInnerClass2(override val val2: Int): MyInterface2 {
        override fun myFun() {
            super<MyInterface2>.myFun()
        }
    }
}

Main function from where the two methods are called at the same time -

fun main() {
    println(MyClass.MyInnerClass1(val1 = 1).myFun())
    println(MyClass.MyInnerClass2(val2 = 2).myFun())
}
Payel Senapati
  • 1,134
  • 1
  • 11
  • 27
  • It may well be a solution to your underlying problem, but I'm afraid I can't see how that answers the question you asked, which was how to call the method of both interfaces _at the same time_ — here you're calling them from different classes, implementing different interfaces. – gidds Aug 16 '20 at 14:31
  • http://xyproblem.info Your actual problem was something different than what you actually asked for. – Tenfour04 Aug 16 '20 at 20:49
  • @Tenfour04 well I wanted to be able to call both the interfaces from the main function without the need to comment out one part of the code each time. Also I wanted to keep it organized so that both the interfaces implemented in one class and not separate class. Creating sub class was therefore the optimal solution. – Payel Senapati Aug 17 '20 at 09:40