2

My current class is below :

class MyGcmListenerService : GcmListenerService() {}

GCMListenerService is as below

public class GcmListenerService extends zzb {}

Now i'm looking to extend to another constructor. When i try to do this ,

class MyGcmListenerService : GcmListenerService(), PushDataReceiver {}

I get error

Only one class may appear in supertypelist. This type has a constructor, and thus must be initialized here

The PushDataReceiver is as below

public abstract class PushDataReceiver extends BroadcastReceiver

I've been trying to google

Kotlin adding secondary constructor

Somehow I'm not able to get any assist for my issue.

1 Answer 1

1

Just like Java, Kotlin doesn't support multiple inheritance. You can only extend a single class at a time - although you can implement multiple interfaces. If you need both a GcmListenerService subclass and a BroadcastReceiver, you'll have to create separate classes for them.


To explain the error message you're getting:

Only one class may appear in supertypelist. This type has a constructor, and thus must be initialized here

This error is pointing out that when you extend a class (unlike when implementing an interface), you need to call its constructor by adding parentheses (and if it has constructor parameters, pass those within these parentheses), like you did with the other class:

class MyGcmListenerService : GcmListenerService(), PushDataReceiver() {}

But again, since you can only have one direct superclass, this won't work.

Sign up to request clarification or add additional context in comments.

4 Comments

Can i work with this? open class MyGcmListenerService : GcmListenerService() { class extended : PushDataReceiver() {
You can, but nesting classes like that doesn't provide you much more than just having completely separate classes for the two. Although if what you want is to access parts of the outer class from the nested one, an inner class might be what you're looking for.
class MyGcmListenerService : GcmListenerService(), PushDataReceiver() {} are you sure this can be compiled?
"But again, since you can only have one direct superclass, this won't work."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.