Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kotlin have a 'trait' type?

Tags:

kotlin

I keep finding trait mentioned in Kotlin examples. For example this answer uses trait.

Is the trait keyword used in Kotlin? If it is, where can I find info on it, and if it is not, was it part of the language spec at some point?

like image 230
Mike Rylander Avatar asked Oct 15 '17 17:10

Mike Rylander


People also ask

What is trait in Kotlin?

In Kotlin, interfaces are traits (historically even keyword trait was used instead of interface ). In practice, it means that we can define default bodies for methods and declare properties (but without any actual values so they still have to be overridden in a non-abstract class): interface NumberHolder {

Can interface have properties Kotlin?

Interfaces Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store state. They can have properties, but these need to be abstract or provide accessor implementations.


1 Answers

The keyword trait was a keyword in Kotlin but now it's removed. It was deprecated on the Kotlin M12 release.

You can find more here.

Edit: It was replaced by the interface keyword, see Traits are now interfaces in the notes to M12 release.

like image 102
Giorgio Antonioli Avatar answered Oct 13 '22 03:10

Giorgio Antonioli