Ansh
01/19/2018, 1:07 PMopen class A {
fun f() {
print("A")
}
}
interface B {
fun f() {
print("B")
}
}
class C: A(),B {
}
Now, this code doesn't compile because class 'C' extends class 'A' and implements interface 'B' and both of them have functions 'f'. So, as soon as I override 'f' function in class 'C', it throws a compilation error that 'f' cannot be overridden because 'f' is not open in class A. Now a similar set up in Java would have worked easily.
class A {
final public void f() {
System.out.println("A");
}
}
interface B {
default void f() {
System.out.println("B");
}
}
class C extends A implements B{
}
Wouldn't it be better if Kotlin also worked the same way as default method of Java interfaces work i.e pick up the default implementation only when no other implementation is available? And suppose if both class 'A' and interface 'B' are from a library, I won't be able to change the function 'f' to open. In that case it would be impossible for me to create the class 'C'. Does anybody here know what is the possible advantage of having this sort of implementation in Kotlin?marstran
01/19/2018, 1:09 PMAnsh
01/19/2018, 1:09 PMgildor
01/21/2018, 4:54 AMAnsh
01/23/2018, 7:13 PM