File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing Implementations Through Interface References" Watch "Accessing Implementations Through Interface References" New topic
Author

Accessing Implementations Through Interface References

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 45
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
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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: 14150
    
  18

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 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Implementations Through Interface References