Instantiate an Interface [duplicate]

筅森魡賤 提交于 2019-12-02 13:39:37

It's generally good practice to depend on abstract types (interfaces or abstract classes) within a system.

In your example, you could indeed write:

SimpleDoc mydoc = new SimpleDoc()

However the problem is that code that uses mydoc will depend on the concrete type SimpleDoc. This isn't necessarily a problem in itself, however, suppose you create a new implementation of Doc, say ComplexDoc.

You'd change your declaration to:

ComplexDoc mydoc = new ComplexDoc();

Now all the places methods that you pass mydoc to would also have to change.

However had you used Doc in the first place you'd have a single change to make with:

Doc mydoc = ComplexDoc();

This is particularly useful when you are working with the Collections API, where it's common to switch one implementation of another or when using Mocking in test case.

If you uses a interface instead of implementation class in clients code, you are able to change implementation without changing clients code.

If you write:

SimpleDoc mydoc = new SimpleDoc();

all the further code may depend on details exposed by the implementing class SimpleDoc. But if you write:

Doc mydoc = new SimpleDoc();

the further code my only depend on aspects exposed by Doc, which make the code even work if you decide in the future to write:

Doc mydoc = new ComplexDoc();

A good example for the differences is List, which has at least two implementations:

ArrayList
LinkedList

If you write:

List list = new ArrayList();

you are free to replace it later with:

List list = new LinkedList();

without breaking the code relying on the variable list (assuming you did not used casts or reflection to access implementation specific features of list).

That is what we called as Virtual Method Invocation or Late Binding

Let us look at an example.

public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}

Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:

A Deer IS-A Animal

A Deer IS-A Vegetarian

A Deer IS-A Deer

A Deer IS-A Object

When we apply the reference variable facts to a Deer object reference, the following declarations are legal:

Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d

All the reference variables d,a,v,o refer to the same Deer object in the heap.

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