Can you instantiate an interface in Java

Can you instantiate an interface in Java

I know the quick answer is no. But I don’t understand something.

What's going on here:

SharedPreferences is an open interface:

http://developer.android.com/reference/android/content/SharedPreferences.html

However, we do not use this interface, as I read in books, we do not create a class and do not implement SharedPreferences. \

Instead, we use this API as follows:

 SharedPreferences pref = Context.getSharedPreferences("some value", 0);

http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String,%20int)

So what happens in this code?


I think its kind of getSharedPreferences () creates a SharedPreferences object that we can use and manipulate.

But SharedPreferences is an interface ... and I was told that you need to implement interfaces that do not create them.

What is this in Java?

Thanks so much for all the pointers.

(I'm still trying to learn)

follow along.

Java API, , Public Interface. , ?

+5
5

java. , .

, , getSharedPreferences , . . , , SharedPreferences

+7
 SharedPreferences pref = Context.getSharedPreferences("some value", 0);

- SharedPreferences, - , SharedPreferences       

+3

SharedPreferences , SharedPreferences. ; , . , , , . http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

+3

, , SharedPreferences.

, , - . , . .

+2

SharedPreferencesis an interface implemented in Context. What you see here is an example of abstraction with respect to an interface: by defining your variable prefas the interface that you allow to simplify code changes (your current class is now loosely coupled to the class Contextinstead of putting a direct link to the side of it) .

The object returned Context.getSharedPreferenceswill be of type SharedPreferences.

0
source

All Articles