Is it possible to create an object of an interface in java?

这一生的挚爱 提交于 2021-01-01 02:17:18

问题


In java, an interface contains only the method type, name and parameters. The actual implementation is done in a class that implements it. Given this, how is it possible to create an instance of a interface and use it as if it were a class object? There are many such interfaces, such as org.w3c.dom.Node.

This is the code that I am using:

DocumentBuilderFactory fty = DocumentBuilderFactory.newInstance();
fty.setNamespaceAware(true);
DocumentBuilder builder = fty.newDocumentBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(result.getBytes());
Document xmldoc = builder.parse(bais);
NodeList rm1 = xmldoc.getElementsByTagName("Subject");
Node rm3 = rm1.item(0);

回答1:


You never create an instance of just the interface. You can have a field/parameter/local variable of that interface type, but that's fine - the value that is assigned to such a variable will always be either null or a reference to an instance of some concrete implementation of the interface. The point is that code which deals only with the interface shouldn't need to care what the implementation is.

A good example is Collections.shuffle(List) - I can provide that any list implementation, and it will only use the methods declared in the interface. The actual object will always be an instance of some concrete implementation, but the shuffle method doesn't need to know or care.




回答2:


There are two ways to get a usable interface class. If we have the interface:

public interface Vehicle{
    public void drive();
}

The first is, as you say to have a class which implements the interface:

public class Car implements Vehicle{
    public void drive(){
        System.out.println("Here in my car I feel safest of all);
    }
}
Vehicle car = new Car();
v.drive();

Or you can create an anonymous class:

Vehicle v = new Vehicle(){
    public void drive(){
        System.out.println("Hello world");
    }
};
v.drive();



回答3:


You don't create an Instance of the interface - you create an instance of a class which implements the interface. You can create as many different implementations as you like and choose the one which fits best.

In your code you can use a implementation where an object conforming to the interface is required.




回答4:


The interface is a special class with an abstract type so it can't create an instance of her self because it doesn't have a type.It has some methods and keeps the type of classes the implements her. So you can create only reference variables and they refers to an object and sure the class of the object must implement the interface.




回答5:


This is the same as in

List<String> strings = new ArrayList<String>();

List is an interface, ArrayList an implementing class. This hides the implementation (class), and offers only the requirement/contract (interface). That is a sound way of programming, as you may exchange ArrayList with LinkedList (if you have not many strings in the list.




回答6:


You don't create an instance of an interface, you create an instance of a class which implements an interface.

e.g.

Map<String, String> map = new LinkedHashMap<String, String>();

The Map is an interface and LinkedHashMap is a class which implements an interface.



来源:https://stackoverflow.com/questions/10172823/is-it-possible-to-create-an-object-of-an-interface-in-java

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