17

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.

River
  • 8,585
  • 14
  • 54
  • 67
sij
  • 1,307
  • 7
  • 18
  • 35

6 Answers6

17

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.

SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
  • Regarding multiple inheritance, **ANY** OOA/OOD can be cleanly translated to OOP in Java using only "interface inheritance". Heck, any OOA/OOD could trivially be translated to a language that wouldn't even have the concept of *"implementation inheritance"* (also know as *"code reuse"*, which really hasn't much to do with OO). Yet some people don't understand that Java fully support MI... :( – SyntaxT3rr0r Dec 29 '10 at 14:51
  • OK, so in what way does your answer differ from all the other answers that you apparently downvoted? Because I can't tell the difference, apart from the wiki link you add. – Sean Patrick Floyd Dec 29 '10 at 15:00
  • 1
    uhhm, yes. And at a given moment some minutes ago, all answers except yours were downvoted (so I naturally assumed it was you). For those who have 1000 rep or more: click the score next to an answer to see the upvotes / downvotes. – Sean Patrick Floyd Dec 29 '10 at 15:04
  • btw your profile says you have given 471 downvotes, so I guess I return to my previous assumptions – Sean Patrick Floyd Dec 29 '10 at 15:05
7

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();
    }

}
slartidan
  • 20,403
  • 15
  • 83
  • 131
4

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>();
Riggy
  • 1,347
  • 1
  • 14
  • 26
1

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();
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

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.

jzd
  • 23,473
  • 9
  • 54
  • 76
0

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();
    }
}
Riduidel
  • 22,052
  • 14
  • 85
  • 185