1

Can someone explain to me what the meaning of creating a variable of an Interface class is?

Specifically, what does the following code mean:

InterfaceA x = (InterfaceA) factory.createFactoryObject(zz.CONST);

I have always thought that an interface (in Java) is simply a collection of method declarations whose functionality is defined by classes implementing an interface. For example if I have an interface:

public interface IMoveBehavior(){
    public void move_Along_X();
    public void move_Along_Y();
    public void move_Along_Z();
}

then the class would use this interface as:

public class ABC() implements IMoveBehavior{
    public void move_Along_X(){
        //do whatever
    }
    public void move_Along_Y(){
        //do whatever
    }
    public void move_Along_Z(){
        //do whatever
    }   
}

So, coming back to

InterfaceA x = (InterfaceA) factory.createFactoryObject(zz.CONST);

I really do not understand what exactly this means conceptually. Could someone explain or point me to a resource where I can educate myself?

Thanks!!

JavaFan
  • 1,295
  • 3
  • 19
  • 28
  • 1
    x is a variable pointing to an object of any type that implements the InterfaceA interface – BoDidely Aug 21 '15 at 14:47
  • [Programming to an interface](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – GriffeyDog Aug 21 '15 at 14:59
  • This is called type casting. – Luiggi Mendoza Aug 21 '15 at 15:04
  • @Luiggi, yes I understand that type casting happens with `(InterfaceA) factory.createFactoryObject(zz.CONST)`, that is not my question. My question is: why are we type casting to an interface... – JavaFan Aug 21 '15 at 15:13
  • @GriffeyDog, great link!! – JavaFan Aug 21 '15 at 15:15
  • Your question boils down to typecasting, regardless if you cast to an interface, an abstract class or a non-abstract class. Type casting works the same. If you want/need to know why to program oriented to interfaces rather than to direct known classes e.g. declare your variables as `List` rather than `ArrayList` or `LinkedList`, then the link posted by @GriffeyDog will be the dup. – Luiggi Mendoza Aug 21 '15 at 15:18

3 Answers3

1

You're seeing a case of Polymorphism, which is one of the core concepts that makes Interfaces useful.

InterfaceA x = (InterfaceA) factory.createFactoryObject(zz.CONST);

What's happening here is that factory.createFactorObject(zz.CONST); is returning an Object. I don't know precisely what kind of object it is, but if this code compiles and runs successfully, then the object must at least be of a class that implements InterfaceA. As a result, it has implementations for all the methods defined in InterfaceA.

So you can cast to InterfaceA, and take it as a given that you can use the methods defined in InterfaceA. The underlying JVM is responsible for knowing exactly how those methods were implemented (by the class the Object is instantiated from) and what to do if those methods are called.

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • Thank you, @Xirema for your explanation. so just to make sure I understand: Once you create an object implementing that interface and then cast back to the same interface, then essentially it will be an interface but just with fully defined methods extracted from the object, right? – JavaFan Aug 21 '15 at 15:16
  • @JavaFan please concentrate on type casting. That's what this is all about. – Luiggi Mendoza Aug 21 '15 at 15:19
1

Instances of Interfaces aren't allowed in java. So one can use the factorypattern, to generate an instance of a class that implements the Interface. For example, createFactoryObject might look like this on the inside:

...
class toCreate implements InterfaceA{
    ...//all methods implemented
}

return new toCreate();

, or simply use a provided prototype to generate new instances. The named nested class is just there to make things more clear, might aswell be an anonymous class, or any other class. Basically this is just a way of completely encapsulating members of a class except for the members specified by the interface.

0

The interest is that you can many implementation the same Interface that's mean many behavior for the same Interface a good example is with Data Acces Object

My interface

Interface Dao {
    Entity create(Entity e);

    Entity find(long id);

    Entity merge(Entity e);

    void delete(Entity e);
}

And 2 different implementation

Class JpaDao implements Dao {
    Entity create(Entity e) {
        //use EntityManager
    }

    Entity find(long id) {
        //use EntityManager
    }

    Entity merge(Entity e) {
        //use EntityManager
    }

    void delete(Entity e) {
        //use EntityManager
    }
}

Class JdbcDao implements Dao {
    Entity create(Entity e) {
        //use preparedStatement
    }

    Entity find(long id) {
        //use preparedStatement
    }

    Entity merge(Entity e) {
        //use preparedStatement
    }

    void delete(Entity e) {
        //use preparedStatement
    }
}

That is called polymorphism

Jib'z
  • 953
  • 1
  • 12
  • 23