Who said the interface cannot have code? -- Kotlin interface introduction (KAD 26), kotlinkad

Source: Internet
Author: User

Who said the interface cannot have code? -- Kotlin interface introduction (KAD 26), kotlinkad

By Antonio Leiva

Time: Jun 6, 2017

Link: https://antonioleiva.com/interfaces-kotlin/

 

 

Compared with Java, the Kotlin interface allows you to reuse more code.

 

 

The reason is very simple:You can add code to your interface. If you have tried Java 8, this is very similar.

 

 

The benefit of including code in interfaces is that you can use combinations in a more powerful way.

 

Java 6 Interface

 

 

The problem with the Java interface is that we can only describe the behavior, but cannot implement it.

 

This is sufficient in many cases. When we want to implement a good combination, it will force us to delegate the implementation of this interface to individual objects, so that we cannot solve some situations.

 

 

It also makes the reuse of simple combination code quite complicated.

 

Kotlin Interface

 

 

Kotlin brings us good news: the interface can have code.

 

This means that we can implement a multi-inheritance (to some extent limited ). We can let a class implement several interfaces and inherit the behavior of each interface.

 

 

To compile an interface that contains some implementation methods, you do not need to do anything special:

 

1 interface Interface1 {2     fun function1() {3         Log.d("Interface1", "function1 called")4     }5 }

 

 

Another interface 2 implements another function:

1 interface Interface2 {2     fun function2() {3         Log.d("Interface2", "function2 called")4     }5 }

 

A class that implements them can use both of them:

 

1 class MyClass : Interface1, Interface2 {2     fun myFunction() {3         function1()4         function2()5     }6 }

 

 

Great! This gives us more functionality when organizing our code.

 

Unable to maintain the interface status

 

 

Remember this very important restriction.We can add code to the interface, but it cannot be stateful..

 

 

This means that we cannot create attributes in the interface to store the status. If we define attributes in an interface, the class implementing this interface needs to override this interface.

 

 

Let's look at an example. Assume that the interface requires a context:

1 interface Toaster {2     val context: Context3 4     fun toast(message: String) {5         Toast.makeText(context, message, Toast.LENGTH_SHORT).show()6     }7 }

 

 

This code is relatively simple. This is an interface for displaying the Toast method. It requires context to do this.

 

If an activity uses this interface, it needs to overwrite the context:

1 class MyActivity : AppCompatActivity(), Toaster {2     override val context = this3 4     override fun onCreate(savedInstanceState: Bundle?) {5         super.onCreate(savedInstanceState)6         toast("onCreate")7     }8 }

 

We specify the Activity itself as the context, and the interface uses it. That's simple.

 

Now, you can use the Toaster function in the Activity without any problems.

 

API delegate

 

Another interesting feature of Kotlin is interface delegation. It is a very powerful tool used to achieve a more clean combination.

 

Assume that you have A class C, which consists of objects of Class A and Class B:

 1 interface A { 2     fun functionA(){} 3 } 4  5 interface B { 6     fun functionB(){} 7 } 8  9 class C(val a: A, val b: B) {10     fun functionC(){11         a.functionA()12         b.functionB()13     }14 }

 

Class C uses functions A and B in its own code.

 

 

If an object is composed of other components, it can directly use the functions of those components.

 

 

This code is written in another way to get the same result, namely, using the interface delegate:

 

1 class C(a: A, b: B): A by a, B by b {2     fun functionC(){3         functionA()4         functionB()5     }6 }

 

 

You can see that class C implements A and B, but it is actually delegated to the object and received as A parameter.

 

By using interface delegation, this class can directly use functions from the implementation class,And still delegate the implementation to other objects.

 

Conclusion

 

 

We have seen the difference between the Java interface and Kotlin. Now try to find out under what circumstances we can simplify our lives, because new ideas can open up a possible new world.

 

In this way, your code is more reusable and easier to read than before.

 

 

Do you still believe that Kotlin can be used for Android development? Start as soon as possible! Thanks to the previous series of articles, you can learn more about Kotlin or create a complete application from scratch in this book.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.