Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can this Kotlin Object Inherit from itself?

I am trying to interface with TeamCity using Kotlin.

When you convert a project to Kotlin (from .xml) you will have a file called Project.kt which is where you can set all your configurations.

With no editing mine looks like this:

object Project : Project(/* Some Function Literal with Receiver */)

This looks like circular inheritance? There are imports that I am omitting, but surely that wouldn't make that big of a difference? Could the name be interpreted in different ways depending on where it appears in the file?

My mind is interpreting the object signature like this:

  • object = Object declaration for singleton.
  • Project (first occurrence) = Name of object.
  • : = Inheritance marker.
  • Project (second occurrence) = Base class to inherit from.
  • () = Constructor call to the base class.

Is this circular inheritance or have I missed something big about Kotlin? I have looked here and here and can't seem to find my answer.

like image 544
ZoSal Avatar asked Oct 13 '17 14:10

ZoSal


People also ask

Can we inherit a object in Kotlin?

Android Dependency Injection using Dagger with Kotlin Inheritance is one of the key features of object-oriented programming which allows user to create a new class from an existing class. Inheritance we can inherit all the features from the base class and can have additional features of its own as well.

How does inheritance work in Kotlin?

In Kotlin, implementation inheritance is regulated by the following rule: if a class inherits multiple implementations of the same member from its immediate superclasses, it must override this member and provide its own implementation (perhaps, using one of the inherited ones).

Can Kotlin class inherit from multiple classes?

First, like Java, a Kotlin class can only inherit one superclass, but it can implement multiple interfaces. Kotlin uses the colon character “:” to indicate both inheritance and interfaces' implementation. So, for example, class MyType(…) : SuperType(…) means the MyType class inherits the SuperType class.

Which inheritance is not supported in Kotlin?

In Kotlin we are not allowed to extend multiple superclasses. Multiple-inheritance is achieved by giving concrete functions to interfaces.


1 Answers

Assuming qualified classes other.Project and my.Project, you'd have two different classes defined with the same name, in different packages.

Since my.Project is defined as a Singleton, you cannot extend from it and the actual base class is other.Project. Kotlin is clever enough to differentiate. If you would try to do the same with a class declaration, you'd get a circular inheritance.

You could even try to force extending from the Singleton explicitly, but then you'll have the error Cannot inherit from a Singleton. So basically this only works well if you imported the right classes.

like image 187
tynn Avatar answered Oct 16 '22 14:10

tynn