wood burning stoves*
The moose likes Java in General and the fly likes Confused about interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Confused about interfaces" Watch "Confused about interfaces" New topic
Author

Confused about interfaces

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi,

I am using JPA to persist entities into a database. I know how to use JPA but I don't understand how its interfaces work. Take the following example:



This is a very simple JSF backing bean. The PersonEntityJpaController class persists the PersonEntity entity to the db. My question is about the injected EntityManagerFactory and UserTransaction. It's my understanding that when you use these your application server (glassfish in my case) "injects" instances of these interfaces into your class. But how can you instantiate an interface? I understand that you can use an interface as a reference type but I am not sure how this works here. A segment of the PersonEntityJpaController class is below:



The UserTransaction and EntityManagerFactory are passed to the constructor and the EntityManagerFactory creates an EntityManager using its createEntityManager method. You can also see that the UserTransaction begin, commit, etc. method being called. My problem is understanding how these methods work when all these are interfaces?? Interfaces only outline methods, they don't write how they are perform (for want of a better word). So what class(s) have implemented these interfaces and their methods, and where are they?

Sorry for the long winded question!

Thanks,
Alan
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

When the factory returns "an instance of an interface", what it's actually doing is returning an instance of a specific class that implements the interface.

If you try logging the value of emf.getClass().getName() (for example) you'll be able to find out exactly which class is being used. But the point of interfaces is that you don't need to know this. All you need to know is that the object you've got implements that interface - your code is completely decoupled from the specific type being used. This makes your code more flexible - if the factory is changed and now returns a different type, your code continues to work.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7894
    
  21

Alan Smith wrote:Sorry for the long winded question!

No probs, but please don't put excessively long lines inside code tags. I broke up yours (line 25 in the second set) this time, but please re-read the UseCodeTags page thoroughly for details.

Thanks.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Matthew Brown wrote:When the factory returns "an instance of an interface", what it's actually doing is returning an instance of a specific class that implements the interface.

If you try logging the value of emf.getClass().getName() (for example) you'll be able to find out exactly which class is being used. But the point of interfaces is that you don't need to know this. All you need to know is that the object you've got implements that interface - your code is completely decoupled from the specific type being used. This makes your code more flexible - if the factory is changed and now returns a different type, your code continues to work.


Ok, thanks for that. I used the emf.getClass().getName() and it printed an EntityManagerFactoryWrapper class. Good to know.
 
GeeCON Prague 2014
 
subject: Confused about interfaces