Overriding properties from interface


#1

While going through the source code of khttp library I found myself having issues to grasp the concept of overriding properties from interface.  An example would be the Request interface and its implementaation GenericRequest from mentioned project.

The constructor of GenericRequest declares several properties, however only some are marked with the override annotation. What would be the use case when I want to override the property? It would make sense to me if Request was not an interface.

Any kick in the right direction (e.g docs) is appriciated.


#2

If a property is defined in the interface, you have to override it in a class that implements the interface. (In effect, for a val property "foo", the interface defined a method "getFoo()", which needs to be implemented in the class implementing the interface.) The question is not whether you want to override them or not - you always need to do that, like with any other method defined in the interface.

The constructor of GenericRequest does not “declare several properties, however only some are marked with the override annotation”. It declares a number of parameters, some of which are immediately converted into properties using the “val” keyword, and those properties implement the corresponding properties from the base interface through the “override” keyword. Other properties of Request are implemented inside the body of GenericRequest.

We discuss this in some more detail in our book: https://www.manning.com/books/kotlin-in-action