https://kotlinlang.org logo
Title
r

rrader

06/17/2019, 3:00 PM
interface Some {
  fun calculate(str: String? = null)
}
class SomeImpl : Some {
  fun calculate(str: String? = null)
}
An overriding function is not allowed to specify default values for its parameters Why?
l

Luke

06/17/2019, 3:07 PM
That does not stop you from calling it without parameters, no?
The overloading is “handled” by the interface
r

rrader

06/17/2019, 3:10 PM
but why I can not specify same default parameter in implementation?
l

Luke

06/17/2019, 3:11 PM
I guess it’s to not break the interface’s contract
s

streetsofboston

06/17/2019, 3:19 PM
@rrader That would violate one of the SOLID principles, in this case the L(iskov substitution principal).
r

rrader

06/17/2019, 8:10 PM
How it can break if it is the same default value?
s

streetsofboston

06/17/2019, 8:17 PM
If it is the same, you don’t have to specify it in its implementation.
r

rrader

06/17/2019, 9:08 PM
I open the implementation class and I want to see that it can have a default parameter, if I will not open the interface than I will not know this
s

streetsofboston

06/17/2019, 9:10 PM
But the code that calls the
calculate(...)
functions, calls it on a variable that is declared to be of type
Some
, correct, not of type
SomeImpl
?
val mySome : Some
...  
...
mySome = ... SomeImpl() ...
...
mySome.calculate()
r

rrader

06/18/2019, 6:33 AM
it could be also a type of
SomeImpl
, why not?
s

streetsofboston

06/18/2019, 11:51 AM
Yup, but usually it won't be for the users of your
Some
instances (they should not be concerned with the specific implementation of an interface).