0

I have an interface called Functions without any method defined in it. Then I have an implementation class that implements that interface and has also a method defined in the implementation class. If I create a variable of the interface type and assign it with a new instance of the implementation type (which has a method defined in it). Why can't I access that method from the variable? I think I'm missing something here. I was under the impression that if the variable of the interface type has been assigned an instance of the implementation type which has a method defined in it, than that variable can be used to run the method.

Please advise. Thank you in advance.

1
  • 3
    It'd be great if you provided a small, self-contained example code of your problem. You will get answers more quickly. Commented Oct 17, 2013 at 11:43

4 Answers 4

2

Conceptually, you are doing the wrong thing here.

If you want to call "that method" then you should use a variable of the implementation type, not the interface type.

Alternatively, if "that method" really does belong in the intended functionality the interface, then you should move it "up" to the interface.

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I can understand, your problem is the following:

// Interface with no methods
public interface Functions {
}

// Implementation class with a method defined in it
public class Implementation implements Functions {
    public void foo() {
        System.out.println("Foo");
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a variable from the interface type and
        // assign a new instance of the implementation type
        Functions f = new Implementation();
        // You try to call the function
        f.foo();     // This is a compilation error
    }
}

That's the correct behavior, this is not possible. Because the compiler sees that variable f has the (static) type of Functions, it only sees the functions defined in that interface. The compiler is not aware of whether the variable actually contains a reference to an instance of the Implementation class.

To solve the issue you either should declare the method in the interface

public interface Functions {
    public void foo();
}

or make your variable have the type of your implementation class

Implementation f = new Implementation();

2 Comments

Then why do people use List<String> mylist = new ArrayList<String>();? What is the benefit of that? Why not just make the mylist variable of type ArrayList instead of the List interface? Also If you can explain why it would be beneficial for me to make the variable f of type Function instead of Implementation? What are some use cases in which what I'm trying to do would be useful?
They do that to be able to change the List implementation easily if their code would need other type of list (linked list, queue) for more optimal operation. If they use only the methods defined in the List interface then they can change implementation to ArrayList, LinkedList, etc. without modifying the rest of the code.
1

You are restricted to the methods defined by the Reference type, not the Instance type, for example:

AutoClosable a = new PrintWriter(...);
a.println( "something" );

Here, AutoClosable is the reference type and PrintWriter is the instance type.

This code will give a compiler error because the only method defined in AutoClosable is close().

2 Comments

Then why do people assign variables of type interface instead of the implementation type? That's all I'm trying to do here to try and code to an interface. Learning design patterns while I try this stuff.
Looking again at AutoClosable, can you imagine trying to accommodate every class (most of which don't exist yet) that programmers might want use with try-with-resources? No, of course not. On the other hand, it is very easy to imagine handling just one interface, which programmers can implement with their class. In this way, you can handle any class
0

You cannot do that, consider this example:

interface Foo {

}

And class:

class FooBar implements Foo {
   public void testMethod() { }
}

class FooBarMain {
    public static void main(String[] args) {
       Foo foo = new FooBar();
       //foo.testMethod(); this won't compile.
    }
}

Because at the compile time, compiler will not know that you are creating a new FooBar();and it has a method called testMethod() which will be determined dynamically. So it expects whatever you are accessing via interface variable should be available in your interface.

What you can do is if you want to access that method via interface variable, it's better to move that method to interface and let clients implement it.

Let me know if you have doubts on this.

2 Comments

Then why do people use List<String> mylist = new ArrayList<String>();
@ImtiazAhmad, that is also same, List<String> is an interface and ArrayList is a concrete implementaion of List, and of-course it contains implementations of all of the methods available in List interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.