philomath
philomath

Reputation: 2209

SharedPreferences.Editor (Android) confusion

To be honesr, I'm not really sure how to set the title for this. I'm learning http://developer.android.com/training/basics/data-storage/shared-preferences.html and would like to make things clear a little bit.

Lets say we have:

SharedPreferences sharedPref = getSharedPreferences(USERNAME, MODE_PRIVATE);

and

SharedPreferences.Editor editor = sharedPref.edit();

When I hover the mouse on edit() (I use Eclipse IDE), I learned that

Open Declaration Editor android.content.SharedPreferences.edit()

public abstract SharedPreferences.Editor edit ()

Added in API level 1

Create a new Editor for these preferences, through which you can make modifications to the >data in the preferences and atomically commit those changes back to the SharedPreferences >object.

Note that you must call commit() to have any changes you perform in the Editor actually show up in the SharedPreferences.

Returns Returns a new instance of the SharedPreferences.Editor interface, allowing you to modify the values in this SharedPreferences object.

So I actually have 3 small questions here:

1/ edit() is an abstract method, so in what class has edit() been overriden (I've already learned that "public abstract SharedPreferences.Editor edit()" located in SharedPreferences interfaces, so there must be some class out there implements this interface)

2/ SharedPreferences.Editor is an interface. As far as I known, an interface can not be instantiated. How come this editor object above can receive "a new instance of the SharedPreferences.Editor interface" as stated in the API.

3/ I just start to learn Android for a few days, is it a good approach that I learn in this way, which mean dissecting everything to make them clear to me?

Thank you

Upvotes: 3

Views: 641

Answers (1)

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23972

Main thing that you should understand, is that you shouldn't care who and how implements abstract class (or interface) that you receives. In case of SharedPreferences, it implemented by Android internal class (which means it's not available to you) SharedPreferencesImpl.

In other words, you shouldn't care about details. Interface just describes what this object can do (and you should just believe in it).

P.S. As far, as you're learning something - it is a good approach.

Upvotes: 1

Related Questions