Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property initializers are not allowed in interfaces

My Java project has an ItemType interface with the given values, which is implement by some classes. How can I implement this interface on Kotlin?

public interface ItemType {

int TYPE_OPTION = 2;
int TYPE_GRID = 3;
int TYPE_CAROUSEL = 4;
int TYPE_MUSIC = 5;
int TYPE_GUESS = 6;

int getItemType();

}
like image 775
P1NG2WIN Avatar asked Aug 01 '19 15:08

P1NG2WIN


People also ask

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.

Can we define properties in interface Java?

Properties of interfaceIt always contains final data members. It cannot be instantiated. All methods of interface are abstract and public in nature. The class which implements interface need to provide functionality for the methods declare in the interface.

Does Kotlin allow field variables in interfaces?

Kotlin Interface cannot have initialized variables. Kotlin Interface can have non-abstract and abstract methods.

How do I extend an interface in Kotlin?

In Kotlin we use a single colon character ( : ) instead of the Java extends keyword to extend a class or implement an interface.

Why interfaces don't have static initialization block when it can have?

Why interfaces don't have static initialization block when it can have static methods alone in java? An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. A static method is declared using the static keyword and it will be loaded into the memory along with the class.

Are property initializers allowed in Kotlin interfaces?

It results in this error at the interface instance variable line: Kotlin: Property initializers are not allowed in interfaces . How can I initialize instance variables in Kotlin interfaces?

Can an interface have a default implementation for a property?

Beginning with C# 8.0, an interface may define a default implementation for members, including properties. Defining a default implementation for a property in an interface is rare because interfaces may not define instance data fields.

When do you need an explicit interface member implementation?

For example, if the class Employee is implementing two interfaces ICitizen and IEmployee and both interfaces have the Name property, the explicit interface member implementation will be necessary. That is, the following property declaration: implements the Name property on the IEmployee interface, while the following declaration:


Video Answer


1 Answers

You can use the companion object:

interface ItemType {
    val itemType: Int

    companion object {
        const val TYPE_OPTION = 2
        const val TYPE_GRID = 3
        const val TYPE_CAROUSEL = 4
        const val TYPE_MUSIC = 5
        const val TYPE_GUESS = 6
    }
}
like image 125
gpunto Avatar answered Oct 15 '22 16:10

gpunto