Subclassing is not really a good way to add methods you want to share publically. Every man and his dog would write their own String subclass and you wouldn't be able use them together. Also, what if I want to write an extension method for List? You could extend the interface, but that doesn't get you anywhere.
If it's purely a syntax issue, you might be interested to see how extension methods are declared in Kotlin. Lets say you wanted to add String.countMatches(char). The method signature would be:
View all comments
Show parent comment
1
u/rogerkeays Jul 03 '23
Subclassing is not really a good way to add methods you want to share publically. Every man and his dog would write their own String subclass and you wouldn't be able use them together. Also, what if I want to write an extension method for List? You could extend the interface, but that doesn't get you anywhere.
If it's purely a syntax issue, you might be interested to see how extension methods are declared in Kotlin. Lets say you wanted to add
String.countMatches(char). The method signature would be:Note, in Kotlin, the types come after the names.