Confusing inheritance

I have this small snippet: https://pl.kotl.in/SxRDgDBOI

interface A {
    val value: String
}

abstract class B(val value: String)

class C : B(value = "Foo"), A

I came across this set up of interface and classes in on of my projects. By accident the variable name and type matched and I didn’t have to override value in the implementing class C. This seemed strange to me and I just wanted to post this here to confirm this is the right behavior.
It seems strange because the abstract class B doesn’t know or reference the interface A in any way.

Edit:

Actually, what about this: https://pl.kotl.in/PzI2MPVvr

Am I missing something? I never encountered that before.

Play with java interfaces and classes in addition to it :slight_smile: It works the same way for methods (val has getValue() semantics so it repeatable in java).

interface A {
    String getValue();
}

class B {
    public String getValue(){
        return "1234";
    }
}

class C extends B implements A {
    // no need to override
}

When you make two interfaces with exactly same method description - it’s only one method description for implementing class. You can use it for any number of “intersected” interfaces . When you make one class and one interface you have delegate (by the will of inheritance merging strategy as in interfaces) just like if you make B: A and C(b: B): A by b but through the inheritance, not composition. So you don’t need to override already defined method.

2 Likes

Ok thanks. That’s something new I learned today. Nice.