-1

EDIT: changed methodA to someMethod everywhere

In this tutorial, an interface has been defined like this:

public interface MyInterface{
    public int someMethod();

    public class ClassA implements MyInterface{
        @Override
        public int someMethod(){
            //codeA
        }
    }

    public class ClassB implements MyInterface{
        @Override
        public int someMethod(){
            //codeB
        }
    }

}

Then they have passed an instance of the above interface in a method of another class like so:

public class MyAnimationClass{

    static void animate(final MyInterface myInterface){
        myInterface.someMethod();
    }

}

How does this instantiation of an interface work? Moreover, in the statement myInterface.someMethod();, someMethod() of which class would be called by default - ClassA or ClassB?

Price
  • 2,683
  • 3
  • 17
  • 43
  • Have you heard of *polymorphism* ? – Konstantin Yovkov Oct 10 '14 at 11:37
  • Read this: http://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used – Daniel Pereira Oct 10 '14 at 11:37
  • Where do you see an "instantiation of an interface"? I just see a *parameter* with an interface type. Perhaps you need to review the tutorial on interfaces? http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html – Jon Skeet Oct 10 '14 at 11:37
  • `Polymorphism `and `dynamic binding`. – Milaci Oct 10 '14 at 11:40
  • Can you please answer the second more important question? While executing the statement `myInterface.methodA();`, would the method definition in ClassA be used by default or the one in ClassB? – Price Oct 10 '14 at 11:43
  • I don't see any method `methodA()`, is only `someMethod()` in the interface declaration. All depends of what you pass to `animate` method when you instantiate a `MyAnimationClass`. If you pass ClassA is execute a method `someMethod()` of ClassA. – Milaci Oct 10 '14 at 11:49
  • Sorry, I meant someMethod(). someMethod() methods of both ClassA and ClassB are identical in return types and in their parameters - in this case, which definition would be used by default in MyAnimationClass? – Price Oct 10 '14 at 11:53
  • By default is nothing!! If you don't pass to it. – Milaci Oct 10 '14 at 11:54
  • MyAnimationClass ma=new MyAnimationClass(); ma.animate(new ClassA()); in this case when you call myInterface.someMethod(); is a call a someMethod() of ClassA. – Milaci Oct 10 '14 at 11:55
  • Why do you need two classes that make same thing? Use only one! – Milaci Oct 10 '14 at 12:05
  • Was trying to understand this implementation by the Google Maps team: https://gist.github.com/broady/6314689. Got it now, thank you all! I was familiar with polymorphism, less so with things that can not be instantiated - not a professional programmer so bear with me please. – Price Oct 10 '14 at 12:10
  • Some suggestion: vote the correct answer, so who see this question can find a correct answer immediately! – Milaci Oct 10 '14 at 12:17

2 Answers2

0

You don't instantiate an Interface, you instantiate a class implementing the interface. The code using the object only needs to know the Interface, not the implementing class.

This is what happens in the method call. The method knows that the parameter implements the interface. It can now call all methods defined in the interface. Which method actually gets calls depends on the implementing class. If I pass an object of ClassA to the method, the methodA() of ClassA will be called. If I pass a ClassB object, it will call the methodA() of ClassB.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
0

This is basic OO, I recommend reading a book on the subject to further familiarize yourself with the concepts.

To answer your question, you do not instantiate the interface. You instantiate a class that implements the interface.

In the tutorial you mention, the LatLngInterpolator is never instantiated. outside of the example code, the Linear, LinearFixed or Spherical classes are instantiated.

Then at some point they are passed to the

 static void animateMarkerToHC(final Marker marker, final LatLng finalPosition, final LatLngInterpolator latLngInterpolator) 

method which only sees the Interface aspect of it.

a simple example you can run on your local machine:

public class InterfaceExample {

public static void main(String[] argv) {
    PrintApple applePrinter = new PrintApple();
    PrintPear pearPrinter = new PrintPear();

    printObject(applePrinter);
    printObject(pearPrinter);
}

public static void printObject(PrintInterface p) {
    p.printMe(System.out);
}

public static interface PrintInterface {
    public void printMe(PrintStream p);
}

public static class PrintApple implements PrintInterface {
    public void printMe(PrintStream p) {
        p.println("apple");
    }
}

public static class PrintPear implements PrintInterface {
    public void printMe(PrintStream p) {
        p.println("pear");
    }
}

}

observe that as far as the printObject method can detect it gets PrintInterface. the compiler ensures, bar explicit casting, that the object you put in must implement this interface.

this is very close to the tutorial you are seeing.

I think one thing you got confused over was the fact that inside the interface, other classes were being defined. This is a common practice which you should read up on. (inner classes, static inner classes)

Joeblade
  • 1,735
  • 14
  • 22
  • Okay, so while interpolating a marker I would instantiate the Spherical class and pass the Spherical object to animateMarkerToGB() method, right? – Price Oct 10 '14 at 12:03
  • Yes, before calling the method that requires an interpolator, you'll need to contruct an instance of spherical interpolator and pass it along. – Joeblade Oct 13 '14 at 07:08