devoten
devoten

Reputation: 11

KOTLIN How to extend superclass of existing class

I have a helper class to get network updates. When I try to create secondary constructor it throws an error.

Based on the documentation for kotlin I need to extend the super class. But I get the same error.

As per the documentation, ConnectivityManager does not have a constructor.

I get this error:

Supertype initialization is impossible without primary constructor

My helper class:

class InternetConnectivityHelper : ConnectivityManager.NetworkCallback() {

    constructor(context: Context, internetStatusChangedListener: InternetStatusChangedListener): super(){

    }
}

I also tried the following based on one of the stackoverflow answers:

class InternetConnectivityHelper : ConnectivityManager.NetworkCallback() {

    constructor(context: Context, internetStatusChangedListener: InternetStatusChangedListener): this(){

    }
}

Upvotes: 1

Views: 185

Answers (1)

Jakir Hossain
Jakir Hossain

Reputation: 3930

Try like this

Add parentheses with class name for primary constructor.

class InternetConnectivityHelper() : ConnectivityManager.NetworkCallback() {

    constructor(context: Context, internetStatusChangedListener: InternetStatusChangedListener): this(){

    }
}

OR

Remove parentheses from the class name to define secondary constructor without defining the primary constructor.

class InternetConnectivityHelper : ConnectivityManager.NetworkCallback {

    constructor(context: Context, internetStatusChangedListener: InternetStatusChangedListener): super(){

    }
}

Upvotes: 2

Related Questions