https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Ayfri

07/12/2021, 5:44 AM
Hi, how can I set a function abstract for a companion object of a class ?
s

Shawn

07/12/2021, 6:01 AM
how would that work? are you trying to do something that’d look like this?
Copy code
class Foo {
  companion object {
    abstract fun bar() {}
  }
}
a

Ayfri

07/12/2021, 6:19 AM
Yeah exactly, but without braces for the
bar
function logically
e

ephemient

07/12/2021, 6:23 AM
companion/singleton objects can't be extended, so what would that even mean?
a

Ayfri

07/12/2021, 8:00 AM
the class also has to be abstract I forgot
d

diesieben07

07/12/2021, 2:16 PM
The companion object is still a singleton and cannot be extended
d

Dave K

07/12/2021, 5:53 PM
You can make an interface that the companion implements but you’d still need to remember to implement the interface explicitly on the concrete class, no way to have abstract class enforce that concrete’s companion does anything Main benefit of having an interface is so that all of the concretes’ companions have the same function similar to having an abstract fun would do, at the class level