Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

interface

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.

like image 896
sij Avatar asked Dec 29 '10 14:12

sij


People also ask

What happens when we call a method of interface?

In order to call an interface method from a java program, the program must instantiate the interface implementation program. A method can then be called using the implementation object.

Can we give method body in interface?

There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

What happens when a class implements an interface?

When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. A class uses the implements keyword to implement an interface.

Can an interface may have a method implementation?

Interface Methods As mentioned earlier, the interface cannot specify any implementation for these methods. It is up to the classes implementing the interface to specify an implementation. All methods in an interface are public, even if you leave out the public keyword in the method declaration.


2 Answers

Sure, your code works fine! You just have to initialize your variable interface1 with an actual implementation (i.e. 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();
    }

}
like image 35
slartidan Avatar answered Nov 02 '22 21:11

slartidan


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.

like image 99
SyntaxT3rr0r Avatar answered Nov 02 '22 20:11

SyntaxT3rr0r