June 2018
Intermediate to advanced
310 pages
6h 32m
English
You may have noticed from the previous examples that String in Kotlin has some methods that its Java counterpart is lacking, such as reversed(). How is that achieved, if it's the same String type as in Java and, as we know, String in Java cannot be extended by any other class, since it's declared final?
If you look at the source code, you'll find the following:
public inline fun String.reversed(): String { return (this as CharSequence).reversed().toString()}
This feature is called an extension function, and it also exists in some other languages, such as C# or Groovy.
To extend a class without inheriting from it, we prefix the function name, reversed in our example, with a class name we want to extend.
Do note that the ...
Read now
Unlock full access