51

I just get started experimenting Android app using Kotlin. I just wanted to inherit Application class like this:

class SomeApp : Application {

}

But the compiler raises the warning:

enter image description here

and suggestion changes it to:

class SomeApp : Application() {
    override fun onCreate() {
        super.onCreate()
    }
}

I read about primary and secondary constructors in the docs. So if super class has a primary constructor, then is it necessary to write here? like Application class has its own constructor

public Application() {
    super(null);
}

then it would be necessary to have primary constructor for derived? or Can't I do something like Java way:

class SomeApp : Application {
   constructor SomeApp(){
      super();
    }
}

or this error suggests something else? Can anyone explain me in detail? I'm very new to the language and this looks weird to me.

Edit: In java I can do the following: class SomeApp extends Application{ }

It has implicit constructor, so I do not have to write: class SomeApp extends Application{ public Application(){ super(); } } But in kotlin do I have to define empty constructor like the following: class SomeApp:Application(){ } ?

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
Krupal Shah
  • 8,949
  • 11
  • 57
  • 93

3 Answers3

68

This is not about primary/secondary constructors.

On JVM (and pretty much anywhere else) a constructor of the base class Application is called when you create an instance of SomeApp

In Java the syntax is like you said:

class SomeApp : Application {
    constructor SomeApp(){
      super();
    }
}

Here you must declare a constructor, and then you must call a constructor of the super class.

In Kotlin the concept is exactly the same, but the syntax is nicer:

class SomeApp() : Application() {
    ...
}

Here you declare a constructor SomeApp() without parameters, and say that it calls Application(), without parameters in that case. Here Application() has exact the same effect as super() in the java snippet.

And in some cases some brackets may be omitted:

class SomeApp : Application()

The text of the error says: This type has a constructor, and thus must be initialized here. That means that type Application is a class, not an interface. Interfaces don't have constructors, so the syntax for them does not include a constructor invocation (brackets): class A : CharSequence {...}. But Application is a class, so you invoke a constructor (any, if there are several), or "initialize it here".

voddan
  • 31,956
  • 8
  • 77
  • 87
  • What you mean? That 's all there is to it – voddan Dec 13 '15 at 14:34
  • @KrupalShah I edited the answer. Is it what you were asking about? – voddan Dec 13 '15 at 16:12
  • 1
    So to my understand, there is no implicit empty constructor. Am i right? – Krupal Shah Dec 13 '15 at 17:49
  • 1
    @KrupalShah There is an implicit empty constructor if nothing else is specified, but you have to invoke it explicitly – voddan Dec 13 '15 at 17:51
  • So if an implicit empty constructor exists, why do i need to explicitly say compiler? This looks too weird to me. If you can explain better than please help. – Krupal Shah Dec 13 '15 at 17:54
  • 1
    That's simple: not like constructor declaration, constructor **invocation** is an action, which may have side effects. And no side effects might be implicit, that's the philosophy behind Kotlin – voddan Dec 13 '15 at 17:57
  • Actually, I personally not that sure that implicit constructor was such a great idea. You are not the first to get confused, and maybe that's a sign. – voddan Dec 13 '15 at 17:59
  • Ok thank you very much for helping. One last question: in java I extend Application class, it works well – Krupal Shah Dec 13 '15 at 18:01
  • @KrupalShah You mean explicit? There is an [explicit public nullary constructor in the Application class.](http://developer.android.com/reference/android/app/Application.html#Application()) – Vince Dec 13 '15 at 18:02
  • @voddan how would I initialize a private constructor – Donki Apr 17 '20 at 08:41
8

You don't need

class SomeApp : Application() {
   constructor SomeApp(){
      super();
    }
}

because this is equivalent. And if the class has a primary constructor, the base type can (and must) be initialized right there, using the parameters of the primary constructor.

class SomeApp : Application() {
}

which is also equivalent in java to

class SomeApp extends Application {
     public SomeApp(){
       super();
     }
}
denixtry
  • 2,928
  • 1
  • 21
  • 19
0

Convert

class SomeApp : Application {

}

To

class SomeApp : Application() { // just add ```()``` 

}
CrackerKSR
  • 1,380
  • 1
  • 11
  • 29