From the course: Intermediate Kotlin for Android Developers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Access extension functions from Java
From the course: Intermediate Kotlin for Android Developers
Access extension functions from Java
Even when using Kotlin, there may be times when you need to work with an existing Java class. But we can still take advantage of our extension functions in that scenario as well. Here in our planet class, we have our extension property for getting the composition. Let's create a scratch file to test out how we could use this from a Java class. So we'll do that with the command shift n shortcut. And our file is going to be of type Java. So just like before, we're in a standard Java class and we're going to create a new class that we call sample use. And we'll create a simple method in here of public void use extension. And this is where we're going to put the code in order to test out our Kotlin extension property that we created. So first we're going to need a planet. Let's just name it p. So we'll create a new one here in line six. And now let's try to call the planets composition method. Oh, but look, we don't get anything. Why is that? Well, the compiler doesn't know anything about…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.