开发者

What does it mean for a function to return an interface?

I just saw a member function like this:

public Cat nextCat(GameState state); 

But Cat is an interface like this:

public interface Cat {
        void makeCat(GameState state);
}

So I am confused as to how to interpret this. I know what it means when something retu开发者_开发技巧rns an object or a primitive. But what does it mean to return an interface? How to use this function's return value?


Think about this way: If Cat where a regular class, what exactly would you care about when you wanted to call some methods on it?

You'd care about the method definitions: their names, their argument types, their return values. You don't need to care about the actual implementation!

Since an interface provides all of that, you can call methods on it, just as you can on a regular class.

Of course, in order for the method to actually return some object, there needs to be some class that implements that interface somewhere. But what class that actually is or how it implements those methods doesn't really matter to the code that gets that object returned.

In other words, you can write code like this:

Cat cat = nextCat(GameState.STUFF);
cat.makeCat(GameState.OTHER_STUFF);

That code has no knowledge of the concrete type that implements the Cat interface, but it knows that the object can do everything that the Cat interface requires.


This function returns an object of a class which implements the Cat interface. The implementation details (for this concrete class) are up to you, as long as you implement all the methods of the Cat interface for it.


For example you could do this:

interface Cat {
    String meeeeow();
}

public Cat nextCat(GameState state) {
    return new Cat() {
        public String meeeeow() {
            return "meeeeeeeeeow!!!!";
        }
    };
}

in which case the method nextCat returns an implementation of the Cat interface by means of an 'anonymous-inner-class'. This shows that the code calling nextCat does not need to know which code implements the returned Cat interface. This is an example of one of the key strengths of Object-Oriented programming: because the calling code doesn't know the implementation, the impact of changing the implementation later on is small (as long as the interface remains the same).


This is one way of abstration, "Hiding Actual implementation", The Best Example is Map interface where if some API has return type as Map, then user actually don't need to care about the type of object implementation i.e. 'Hasmap for Hashtable', With the help of methods defined in Map interface, user can perform operation on the returned object because interface create a contract with the implementing classes that the "implemeting class must provide defination for declared method" otherwise declare that implementing class as abstract.


It is worth emphasizing once again that it is perfectly legal—and in fact very common—to have variables whose type is an interface, such as :

Measurable meas;  //"Measurable" is an interface name.


Just remember that the object to which meas refers doesn’t have type Measurable.
In fact, no object has type Measurable. Instead, the type of the object is some class that implements the Measurable interface. This might be an object of the BankAccount class or Coin class, or some other class .

    meas = new BankAccount(1000); // OK 
    meas = new Coin(0.1, "dime"); // OK

What can you do with an interface variable, given that you don’t know the class of the object that it references?
You can invoke the methods of the interface:

double m = meas.getMeasure();

and also you should know that there can be type conversians between class and interface types.


It means, that the member function can return any implementation. This follows the "Program to an interface, not an implementation." design pattern.


Methods do not return interfaces or classes. They return a reference to an instance (=object) or null (or a primitive value, but let's stick with objects). This reference is usually either stored in a variable or used to call instance methods or access instance members.

The declared return type tells us the least specific type of the real instance, whose reference is returned by the method. The object behind that reference may be exactly that type or any subclass (or sub-subclass, ...).


A method can return nothing (void), or a primitive value int, double, ...), or a reference that is either null or refers to an instance. When a method returns a reference, it is declared as returning a reference to some particular kind of object.

In your case, the reference this method returns will either be null or will refer to an object whose class implements Cat


When you don't need to know about implementation you can return interface to leave implementation to that method and caller can just use methods that interface provides.

Cat cat = nextCat(state);
cat.makeCat(state);


As per the object oriented definition, an interface is a group of related methods with empty bodies. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. Having said that, nextCat(GameState state) returns an Interface Cat which means that at runtime it could return any object which Implements Cat. Now, isn't that flexible? Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that clients expect.


Returning interface allows a member function to return reference of any implemented class. It gives flexibility to program to an interface and also is helpful in implementation of factory and abstract factory design pattern.


It return an Object which implements the interface. There are some examples for reference : https://blog.csdn.net/hduxiejun/article/details/52853153

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜