I thought I understood interfaces....then along came a collection

Hi all
I'm having a bit of trouble understanding how the following works:
HashMap vehiclesOnRunway = new HashMap();
public Vehicles[] getVehiclesOnRunway(){
  Collection vehicles = vehiclesOnRunway.Values();
  return (Vehicles[])vehicles.toArray();
}It works ok, but I want to understand how the interface gets its implementation. I would understand if vehicles was a concrete class, or if HashMap.Values() returned a concrete class.
My question is: if vehicles is a Collection (interface) and HashMap.Values() returns a Collection (interface) then where does the implementation come from for Collection.Values()?
Does anybody know the answer to this? Or could you recommend a good book or online source?
Kind regards
Tim

It works ok, but I want to understand how the
interface gets its implementation. I would understand
if vehicles was a concrete class, or if
HashMap.Values() returned a concrete class.And what if Vehicles was an abstract class? Would you understand it then? Because its the same idea.
What if Vehicles was a non-final concrete class? Would you understand it then if the returned reference was to an object whose type was a subclass? (e.g., return type is Shape, the returned reference is toa Square)? Because its the same idea.
You can check the runtime type of the returned value by printing vehicles.class; Then, look at the source code or documentation for the relevant class, and you will see that if implements Collection.
Here's a short example that you can run and that will hopefully help:
interface MyInterface {
    void myMethod();
class MyClass implements MyInterface {
    public void myMethod() {
        System.out.println("MyClass.myMethod called on "+this);
class MyOtherClass implements MyInterface {
    public void myMethod() {
        System.out.println("MyOtherClass.myMethod called on "+this);
public class InterfaceTest {
    private static MyInterface getMyInterfaceObject() {
        int i = (int)(100 * Math.random());
        System.out.println(i);
        if (i % 2 == 0) {
            return new MyClass();
        else {
            return new MyOtherClass();
    public static void main(String[] args) {
        MyInterface obj = getMyInterfaceObject();
        System.out.println(obj);
        obj.myMethod();
}This demonstrates that the code in the main method can call methods on both MyClass and MyOtherClass, because they implement MyInterface. The code in the main method does not know what the actual runtime type of the reference returned from the getMyInterfaceObject method will be - just that it will be a type that implements MyInterface. The getMyInterfaceObject method decides at runtime what type of object to return - either a MyClass or a MyOtherClass; In this case the decision is made according to whether or not a random number is odd or even, but it could be made based on any other criteria, such as looking the type up in a configuration file. Equally, it does not need to be made at runtime, but even if it were made at compile-time (which it usually is), the developer knows that they are free to change the type later and it won't break client code.

Similar Messages

Maybe you are looking for

  • Updating SRM contracts from R/3 to SRM....

    Hi all, I am working in SRM, my requirement is to update the SRM contracts from R/3 to SRM and if any changes happen in R/3 contracts (previously updated contracts to SRM) then it should replicate those changes in SRM contracts also. I found the prog

  • How to sync notes with iOS 4.2.1?

    Hi, since iTunes was updated I miss the possibility to sync notes between my Mac and my iPod Touch 2G with iOS 4.2.1. The function disappeared from iTunes. How can I sync my notes without iCloud (because iOS 4.2.1 does not have iCloud)? Steffen

  • When typing text in forms, the vertical bar gets multiplied all over the place.

    Often when I'm typing text into a form on various sites, be it Facebook or this very site, the vertical bar that follows the input text gets randomized all over the line and appears multiple times. The glitch disappears about half a second after I st

  • Where I can get the javac.exe source code?

    because I wanna to rewrite it in my own way. Thanks

  • Email header printing on separate page

    Not sure what I did, but now when I print an email, the header (to/From:) prints on the first page and then the main email prints from page 2 on.  I can not see where this can be changed.  They used to print together as they do on my other Macs.  Jus