简体   繁体   中英

If I call an interface method, will it get the method body from implementation class and execute?

I have an interface Interface1 . I have its implementation Imple implements Interface1 (all methods have been implemented :) ).

Now, consider a third class CheckCall , can I do a call in the class CheckCall like I mention below :

Interface1 interface1;
interface1.method();

All necessary imports have been done. Please tell me is it possible or not , if not then ok and if yes, then tell me what will happen if I've more than one implementing classes for the same interface and I'm doing the same call.

Well, you cannot call the method directly on the interface, but you can do what you wrote, more or less.

You wrote:

Interface1 interface1;
interface1.method();

This will work if you do this:

Interface1 interface1 = new CheckCall();
interface1.method();

then tell me what will happen if i have more than one impl classes for the same interface and i am doing the same call

Well, that's the nice thing about Java: the problem you're referring to is called the "diamond problem":

http://en.wikipedia.org/wiki/Diamond_problem

And it doesn't really exist in Java because Java fully supports multiple inheritance, but only through "multiple (Java) interface inheritance" (* see comment).

So in your case when you call interface1.method() you're either calling Impl 's method or CheckCall 's method and there's no confusion possible.

Sure, your code works fine! You just have to initialize your variable interface1 with an actual implementation (ie new Imple()).

Check out this example, I used your class-names:

public class CheckCall {

    interface Interface1 {
        void method();
    }

    static class Imple implements Interface1 {
        @Override
        public void method() {
            System.out.println("Imple.method1()");
        }
    }

    public static void main(String[] args) {
        Interface1 interface1;
        interface1 = new Imple();
        interface1.method();
    }

}

Yes, but not as you have it written. You'd have to say:

Interface1 interface1 = new Imple();

You can create a variable of type Interface, and then instantiate it to the class that implements the interface. You see this quite often with the Java collections, for example:

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

Actually, you should refer to methods by the interfaces that define them, but you need an actual implementing class. So I would usually do it like this:

// the variable is of type Interface1
Interface1 interface1 = new Imple();
// but the method will be executed on the Imple object
interface1.method();

No, you need to make the call on the object that implements the interface, not on the interface itself.

Edit: A variable with that type could be used but it would still need to be an class that implements that type. You can't create an instance of an interface.

No.

As you can't instanciate an interface, you'll have to create a Imple object, to use it as an Interface1 one like this :

Interface1 interface1 = new Imple();

interface1.method();

in fact, an interface main interest come from the ability to have a method return any object implementing it, without having to worry about given implementation. in your case, it could shows up as

public class CheckCall {
    public Interface1 factoryMethod() {
        return new Imple();
    }

    public void test() {
        Interface1 used = factoryMethod();

        used.method();
    }
}

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