0

I'm working on some JPA stuff and i'm a little confused with some of the start up code that you have to write.

EntityManagerFactory factory = Persistence.createEntityManagerFactory("sample");
EntityManager manager = factory.createEntityManager();
EntityTransaction transaction = manager.getTransaction();

Those three variables all have an interface as their type. How can we do things like

manager.persist()
transaction.commit()

etc if interfaces cannot be instantiated?

rage
  • 1,777
  • 5
  • 25
  • 36

4 Answers4

2

Interface cannot be instantiated but an interface reference can hold an object of any class implementing that interface. So in your case

EntityManagerFactory factory 

is a reference of interface which is holding an object of the class implenting it , returned by :

Persistence.createEntityManagerFactory("sample");

and hence this statement becomes correct:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("sample");
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

You are correct. Interfaces can not be instantiated but they provide a contract to call methods on objects that implement the interfaces.

So when you for example take a look at the EntityManager, factory.createEntityManager() is returning an object that implements the interface EntityManager. Interfaces make sure that the returned object provides certain required methods.

snrlx
  • 4,987
  • 2
  • 27
  • 34
  • I don't know if I'd say it can be a _very different_ type. It _will_ be an implementation class, but that class _will_ implement `EntityManager` – Colin M Jul 17 '13 at 17:55
  • You are absolutely right. It is confusing to say it like that. – snrlx Jul 17 '13 at 17:56
  • So is this similar to how `List` is an interface but `LinkedList` and `ArrayList` can be pointed to by a variable of type `List` ? – rage Jul 17 '13 at 17:58
  • Yes, LinkedList and ArrayList are concrete implementations of the List interface. – snrlx Jul 17 '13 at 18:01
  • So how can i see which objects that implement those interfaces are being returned? So i can see how they implement the methods that the interface defines. – rage Jul 17 '13 at 18:10
0

I think you are misunderstand what happened here.

  EntityManager manager = factory.createEntityManager(); // here manager is only a reference You are getting that from EntityManagerFactory. 

Now Factory class returns the manager type object. There is no instantiating for interfaces in Java

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

I think this is better seen than expained.

Examples:

public class AttackCommand implements Command {}...
public class DefendCommand implements Command {}...
....

let's say we wanted to add these to a common list of commands. Then you could add these to the list below.

(in a new class)

public ArrayList<Command> commands = new ArrayList();

public CommandManager() {
    commands.add(new AttackCommand());
    commands.add(new DefendCommand());
}

Now here is where that supposed reference comes in. What if we wanted to get a list of the command by name (pretending command has a getName method), or the last attacked target via AttackCommand (pretending it has a LastAttacked method)?.

public void printNames() {
    for (Command cmd : commands) {
        System.out.println(cmd.getName());
    }
}

public Entity getLastAttackTarget() {
    for (Command cmd : commands) {
        if (cmd instanceof AttackCommand) {
            return cmd.lastAttacked();
        }
    }
}

(I know that a map of the commands to just grab by name would be better, but for the sake of the example....)

In essence, it's a better general reference to all things that inherit the interface, but not the interface itself.

Rogue
  • 11,105
  • 5
  • 45
  • 71