4

I'm confused, please help me.

Android developers documentation say SharedPreferences is an Interface and also we can see on grepCode that the SharedPreferences is an Interface.

But developers.android also says SharedPreferences is a Class.

They write.

"The SharedPreferences class provides a general framework ..."

So, SharedPreferences is a Class or an Interface?

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Stanly T
  • 964
  • 1
  • 12
  • 30
  • It's interface http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/content/SharedPreferences.java#SharedPreferences – Ozgur Jun 16 '16 at 10:01

4 Answers4

5

The documentations is not wrong, but may be it is a bit confusing.

If you see the code, you'll see that SharedPreferences is actually an interface. But as you mentioned, here it is referring to its implementation as class which is actually SharedPreferencesImpl and you can look into its implementation here. When you make a call to getSharedPreferences() method, it actually returns the object of SharedPreferencesImpl which is a class that implements SharedPreferences interface.

William Reed
  • 1,717
  • 1
  • 17
  • 30
Rehan
  • 3,270
  • 5
  • 31
  • 30
2

Well, SharedPrefs is an interface. It is implemented by SharedPreferencesImpl which provides us the functionality the doc speaks about shared prefs. Though the SharedPreferencesImpl has not been in the doc primarily, SharedPreferences has always been the face for the functionality.

Neji
  • 6,591
  • 5
  • 43
  • 66
2

SharedPreferences is interface ,but implemented as a class. This conception is abstract factory ,a kind of Design Pattern. The declaration of SharedPreferences is interface, but getSharedPreference() returns a class which implements SharedPreferences interface.

Cenxui
  • 1,343
  • 1
  • 17
  • 25
0

Sharedpreferences is an interface. you can see here that the Sharedpreferences has Interface as its parent.

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share.

For further reference you can see this answer.

Community
  • 1
  • 1
Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • He already has this link he just need to understand what is it.. One link is saying it's an **interface** and the other is saying it's a **class** – Janki Gadhiya Jun 16 '16 at 09:56
  • The second link says "The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types." It's seems like a mistake. – Stanly T Jun 16 '16 at 10:03