Can you instantiate an Interface in Java [duplicate]

烈酒焚心 提交于 2019-12-18 16:54:56

问题


Can you instantiate an Interface in Java

I know the quick answer is "no". But there is something I am not understanding well.

What is happening here:

SharedPreferences is a public Interface :

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

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

Instead we use this API like this:

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

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

So what is really happening in this code ?


I think its like getSharedPreferences() is creating a SharedPreferences object which we can then use and manipulate.

But SharedPreferences is an Interface ... and I was told you have to implement Interfaces not create object of them.

What is this in Java ??

Many thanks for all pointers

(I am still trying to learn)

follow on question.

When I look at a Java API and I see a class as defined as Public Interface. How do I know when to implement that interface or when to create this type of object from it ?


回答1:


You can never instantiate an interface in java. You can, however, refer to an object that implements an interface by the type of the interface.

So what is happening is that getSharedPreferences returns an object that implements that interface. The type of the returned object is not important. What is important is that it implements all the methods for that interface, so it can be used as an SharedPreferences




回答2:


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

in this code the type of your object is SharedPreferences but the instance is a class that implements the interface of SharedPreferences this is a basic concepts of OOP




回答3:


SharedPreferences is a reference, but you are not creating a SharedPreferences object. Rather, you are creating an object that is of that type; namely, an implementation of that type. e.g have a look at the following reference where you can use an Interface as a reference type for an instance which is actually an implementation of the reference type interface. http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html




回答4:


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

You do not create a class that implements this interface, because somebody has already created one for you. You use this class via its interface, without even knowing the class name. That's what interfaces are for.




回答5:


SharedPreferences is an interface implemented in Context. What you see here is an example of abstracting towards an interface: by defining your pref variable as an interface you allow for easier code changes (your current class is now loosely coupled with the Context class instead of putting a direct link towards it).

The object returned by Context.getSharedPreferences will be of type SharedPreferences.



来源:https://stackoverflow.com/questions/15997567/can-you-instantiate-an-interface-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!