This feature is really great. It solves the problem that there are multiple methods in an interface, but the class that implements the interface does not want to implement all the methods. Simply speaking, it is necessary to rewrite the interface when the class needs it. So the following features emerge.

Interface to enhance

Add the following two features to Java8’s medium interface features:

  • You can use the default keyword in an interface to modify default or extended methods; abstract methods cannot be used because of their nature
  • Java8 in Android Studio requires the specified Java version to be configured in the build.gradle module. Of course, if Kotlin is used as the development language, you need to specify the Jvm version for Kotlin, because Kotlin uses Jvm 1.6, so here is how they are configured:
android {
   defaultConfig {
       ... 
       kotlinOptions {
           jvmTarget = '1.8'
       }
   }
   compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
   }
}
Copy the code

After the above configuration, you can use the new features of Java8.

puclic interface onTest{
    void onTestStandardMenthod(a);
    default void onTestDefaultMethond(a){
        // Default logic
    }
    static void onTestStaticMenthod(a){
        // Default logic}}public class TestActivity extends AppCompatActivity implements onTest{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The interface called by the class name
        onTest.onTestStaticMenthod();
          
        Calling code through objects is just an example
        new TestActivity.onTestStandardMenthod() 
        new TestActivity.onTestDefaultMethond()
    }
          
    @Override
    public void onTestStandardMenthod(a){} // Must be implemented
          
    @Override
    public void onTestDefaultMethond(a){} // Can be implemented selectively
          
}
Copy the code

The above code shows how to use it. If you want to know more about the details, you can check the official documentation

Some friends may wonder why I talk about Java first, because Kotlin syntax structure and keywords are not the same, although the implementation of the idea is the same, so I start with Java, and in recent years, THE Jvm version has been upgraded quickly, the basic concept will not change, but there are new features is normal, Those of you who read the documentation often will notice that the way some methods are implemented and the details change.

Let’s move on to Kotlin’s section, which looks similar to Java 8 but doesn’t actually use Java 8’s features, but can be written along similar lines for the sake of understanding.

internal interface onTest {
    fun onTestStandardMenthod(a)
    fun onTestDefaultMethond(a) {
        // Default logic
    }
    companion object { 
        fun onTestStaticMenthod(a) {
            // Default logic}}}class TestActivity  :  AppCompatActivity .onTest{
    override fun onCreate(savedInstanceState: Bundle?). {
    	super.onCreate(savedInstanceState)
      	// The interface called by the class name
    	onTest.onTestStaticMenthod()
    	Calling code through objects is just an example
    	TestActivity.onTestStandardMenthod() 
    	TestActivity.onTestDefaultMethond()
    }
    // Must be implemented
    override fun onTestStandardMenthod(a){} 
    // Can be implemented selectively
    override fun onTestDefaultMethond(a){ super.onTestDefaultMethond()}
}
Copy the code

Kotlin’s interface is similar to Java 8 in that it contains both declarations of abstract methods and implementations. Unlike abstract classes, interfaces cannot hold state. It can have attributes but must be declared abstract or provide an accessor implementation. If you don’t understand Kotlin’s interface, click here for a more detailed description.

Kotlin interface can choose whether to have a method body, by contrast, Java8 needs to declare default can have a method body, static interface method in fact in the implementation of the concept is the same, perhaps said that the current object-oriented change is not part of it, according to the flexibility of the language combined with excellent thinking can write more beautiful code.

For Java8 before it made me think of a friend of ridicule “apple always do some have some function, and then hold conference to exaggerated, fierce” I think Java is like this, some should have over time to improve the grammar or new optimization, should be out of the early, the results have to under the impact of the outside world, Before making a change. When you look at the speed of updates, it’s clear that this isn’t that difficult, and that’s when new languages are born, as is always the case in the world when existing tools get too bad, new tools are born, and of course Java is a part of that.


Welcome to Kotlin Chinese community!

Chinese official website:www.kotlincn.net/

Chinese official Blog:www.kotliner.cn/

Public id: Kotlin

Zhihu Column:Kotlin

CSDN:Kotlin Chinese Community

The nuggets:Kotlin Chinese Community

Jane:Kotlin Chinese Community