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?
6 Answers
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();
}

- 21,129
- 10
- 63
- 81
-
How do I do that without extending a service? – modellero Feb 17 '12 at 15:23
-
@user1102714 I added an example – poitroae Feb 17 '12 at 15:26
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

- 23,650
- 14
- 92
- 146
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

- 648
- 2
- 6
- 19
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.

- 17,426
- 15
- 71
- 93