简体   繁体   中英

Methods That Return an Object of an Interface Type

All beginners like myself, always get confused to see a method returns an object of an interface type, because interfaces have abstract methods, thus cannot be instantiated.

I finally figured out a way to understand this:

((when we say that a method returns an object of an interface type, we are actually implicitly saying that the method in fact returns an object/instance of some class that implements that interface, but in most cases that class is unknown because it is declared as anonymous in the implementation of the method. Thus, we refer to the returned object as being of that interface type.)).

Is this explanation correct ?

"...when we say that a method returns an object of an interface type, we are actually implicitly saying that the method in fact returns an object/instance of some class that implements that interface..." - It is correct, but we are saying it explicitly .

The second part of your definition is quite not correct, as @Jon Skeet pointed out. Applying anonymous class in the implementation is a very specific case. Generally, returning an interface gives you more freedom:

  • It is possible to change implementation of the method to return another object that implements the same interface, without changing code that uses this method .
  • You leave possibility for extending classes to override the method, so that it returns another object that implements the same interface. Here you can actually change the return type. If method of the base class returned concrete class, eg, ArrayList , overridden method would have to also return ArrayList or its subclass.

The rule of thumb is the following. If concrete class implements an interface and there is no benefit in returning a concrete class object, eg, ArrayList , return an interface - List , Collection . This will enhance maintainability of your code, ie, the code will be easier to change in future.

It's my birthday at the end of this month, so I've added a new method to all my friends and family:

public Present givePresent{
    //code to select an appropriate and sufficiently expensive present
    return present;
}

There's two things I could do here. I could write a Present class and ensure that all possible presents extend it. But we could run in to all sorts of problems here: BatmanComic already inherits from ComicBook for example, so we'd have to move further and further up the tree until Present is basically indistinguishable from Object . The other way is to look at what is actually happening here. I'm looking to receive something that fits in to a specific category and, in short, Java has two ways of doing that. Inheritance and Interfaces. Creating Present as an interface is achieving exactly the same goal as creating it as an abstract superclass but avoids all problems of multiple inheritance. This way all I have to do is write an interface:

public interface Present{
}

and make sure all the socks and books and whatever implement that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM