4

This might be a silly question, but I'm struggling on how to make my class extend 2 classes at the same time. I'm trying to make a SERVICE that uses ListActivity. How could I do that?

modellero
  • 213
  • 2
  • 6
  • 14

6 Answers6

1

I assume you are coding in the Java programming language. Then the simple answer is: You don't. Java does not support deriving from multiple classes. Make your ListActivity contain a Service.

class MyService extends Service{
  ...
}

class MyList extends ListActivity{
    MyService service = new MyService();
}
poitroae
  • 21,129
  • 10
  • 63
  • 81
1

Java or android Doesn't Support Multiple Inheritance

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
0

In Java you can't extend more than 1 class at the same time. However, you can implement more than 1 interface (but this is not about your task).

You should look for bound service

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
0

No, it is not possible. You may need to re-architect your implementation.

kosa
  • 65,990
  • 13
  • 130
  • 167
0

You can never actually extend 2 classes directly, you need to use a few tricks and make a few interfaces to get it to work. Here is a guide: http://csis.pace.edu/~bergin/patterns/multipleinheritance.html

feldoh
  • 648
  • 2
  • 6
  • 19
0

Assuming you mean this kind of Service, you cannot do this at all; irrespective of Java's lack of multiple inheritance. Services cannot also be Activities. You should create a Service, and a separate ListActivity, and use a binder to communicate between them.

When you do want to inherit the functionality of two classes, use the delegation pattern.

seanhodges
  • 17,426
  • 15
  • 71
  • 93