Why shouldn't we be able to set the visibility of ...
# announcements
h
Why shouldn't we be able to set the visibility of interface fields and methods? It would be nice for things like this:
Copy code
interface Foo {
    var recentBar: Int

    protected val onBar: (Int) -> Int

    fun bar(x: Int): Int {
        recentBar = onBar(x)
        return recentBar
    }
}
I don't intend this as a suggestion, per say, hence why posting here. I assume there's an important (paradigmatic?) reason I'm missing for why such a suggestion is a terrible idea.
Copy code
All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.
Copy code
All constant values defined in an interface are implicitly public, static, and final
h
I presume you're implying that JB would be unable to make Kotlin's interfaces have this feature because the jvm requires public visibility?
d
it would break Java compatibility
h
Righto. But then my question simply becomes: Why doesn't Java allow for this?
d
also interfaces are meant to define public APIs
j
Java allows public or private. The other two visibilities undermine the concept of an interface, and you should just make a second interface with reduced visibility in that case.
👆 3
e
FWIW (probably obvious), if you don't need multiple inheritance, you could make Foo be an abstract class and do all of these things.
👆 1
p
Interfaces were designed to describe the contract of the class not implementation details that's why there is only public modifier. If you want to have implementation details use an abstract class for that reason.
👆 4
a
I was under the impression interfaces were designed to prevent multiple inheritance and the diamond of death. 😁
h
But muh compositions
Thanks for the replies, everyone. As suspected, I'm a dirty cheater.
p
I was under the impression interfaces were designed to prevent multiple inheritance and the diamond of death.
True but do interfaces really solve those problems? 🙂
h
I just see a lot of potential for cool compositional programming with kotlin's interfaces. With delegation, I almost have multiple inheritance already... D:
a
As I understand, it was the addition of default methods that was supposed to solve the multiple inheritance risk.
h
Yeah and that just makes me hunger for default field values. I've tasted blood, and now I desire the forbidden!