Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing Implementations Through Interface References  RSS feed

Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
lowercase baba
Posts: 12609
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Java Cowboy
Posts: 16081
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Thread Boost - a very different sort of advertising
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!