-1

I was studying a Javadoc and noticed this:

java.lang.Object
    android.content.Context
        android.content.ContextWrapper
            android.app.Service

So it seems, in Android, that Service inherits from Context!

But what is the meaning? They appear as totally separated concepts to me.

Lore
  • 1,286
  • 1
  • 22
  • 57
  • 1
    Technically, the `Activity` class also inherits from the `Context` class. – Edric Sep 09 '19 at 14:42
  • 1
    Check out the Javadoc for the `Context` class for a list of indirect subclasses, as well as an explanation of the class: https://developer.android.com/reference/android/content/Context.html – Edric Sep 09 '19 at 14:43
  • Thank you! I will tell you if it helps – Lore Sep 09 '19 at 14:45
  • In fact, I would rather prefer a possibly simple and concise answer :) – Lore Sep 09 '19 at 14:47

2 Answers2

1

Both Service and Activity inherit from Context. From the Android Documentation: "The Context is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc."

Like an Activity, the Service class needs to be able to get resources and to launch activities and do all the things that need access to application-level classes. Actually, a Service is quite like an Activity, only without the UI element.

Yoav Gibri
  • 158
  • 5
  • 1
    It not seems quite well designed, but your answer fully satisfies me. – Lore Sep 09 '19 at 14:52
  • 1
    You can checkout this great resource by freeCodeCamp for an alternative explanation of the `Context`: https://www.freecodecamp.org/news/mastering-android-context-7055c8478a22/ – Edric Sep 09 '19 at 14:59
  • 1
    What makes you think _this doesn't seem quite well designed_ ? – Arthur Attout Sep 09 '19 at 15:06
  • @ArthurAttout because it seems to me that there are two very unrelated classes... it shouldn't be inheritance. Or am I wrong? – Lore Sep 10 '19 at 07:55
  • 1
    Inheritance doesn't require classes to depict something similar. It can be used for various other reason, and resource-sharing is one of them. Activity and Service both need to access the **Context** of the App. With inheritance, they can retrieve it conveniently by calling `this.getContext()` – Arthur Attout Sep 10 '19 at 08:37
0

The official android documentation describes Context:

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

and Service as

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

Context is actually a broad name, which might be difficult to understand in specific use. You might want to check out a good explanation here.

The inheritance of service from object might be clearer, just imagine context being something in between them, just not as abstract as object.

Ausdroid
  • 26
  • 2