File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Accessing Implementations Through Interface References Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing Implementations Through Interface References" Watch "Accessing Implementations Through Interface References" New topic

Accessing Implementations Through Interface References

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 48
I am not able to understand the highlighted part in the below:

You can declare variables as object references that use an interface rather than a class
type. Any instance of any class that implements the declared interface can be stored in
such a variable. When you call a method through one of these references, the correct
version will be called based on the actual instance of the interface being referred to. This
is one of the key features of interfaces. The method to be executed is looked up
dynamically at run time, allowing classes to be created later than the code which calls
methods on them. The calling code can dispatch through an interface without having to
know anything about the "callee."
This process is similar to using a superclass reference
to access a subclass object

Please help me to understand it.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

Say i have an interface that lists one method:

I then have code that looks like this:

I may not own that methodThatGetsA_myInterface method. It could be a static method given to me by a third party vendor. So today, when I write my code, I get a Foo object that implements the myInterface. I know i can call the doIt method, since that's what the interface says I can call.

Now, 6 months later, my vendor gives me a new jar file. they have created a new class that is BETTER than Foo - the new class Bar. It does what Foo did, but does it 10 times faster and uses 1/3 the memory. Since Bar also implements myInterface, my code that calls doIt still works, even though the Bar.doIt() code didn't exist when I wrote my code.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

So, you understand the first part of the text, before the red part: you can declare variables with an interface type, and then assign a value to them that's an instance of any class that implements the interface. For example:

When you call a method on names, then Java must find the implementation of this method. Note that looking up the actual code for the method happens at runtime, not at compile time.

Because the lookup of the actual method to call happens at the moment the call is done at runtime, it is possible to write and compile programs that call methods through an interface, while at the moment that you write and compile the program, you don't even have a concrete class yet that implements the interface.

Suppose you wrote this code:

What this program does, is load a class dynamically in line 7 and create an instance of it. You specify the name of the class to load on the command line. The class must implement interface NameSupplier.

Note that you can compile this code without even having a class that implements interface NameSupplier.

You can write a class sometime in the future that implements NameSupplier:

You can compile that separately and then run the program:

java NamePrinter MyNameSupplier

The point here is that class MyNameSupplier doesn't even have to exist yet when you write the first program, because the actual method to call (getName in MyNameSupplier) is not looked up when you compile the first program, but when you run it.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Accessing Implementations Through Interface References
It's not a secret anymore!