How Variable of an Interface is Working in JAVA and Calling the Methods of Interface?

Akash

I know all the following rules of Java Interfaces:

  1. You cannot instantiate an interface.
  2. An interface does not contain any constructors.
  3. All of the methods in an interface are abstract.
  4. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  5. An interface is not extended by a class; it is implemented by a class.
  6. An interface can extend multiple interfaces.

Now my question is how we are creating an variable of interface EntityManager and using its methods like given code below:

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateful
public class Movies {

    @PersistenceContext(unitName = "movie-unit")
    private EntityManager em;   // Here declaring a variable of an Interface

    public void addMovie(Movie movie) throws Exception {
        em.persist(movie);      // Here using variable of an Interface to call its method
    }
}

Please put some light on this so that I can clear up my understanding of how this code is working!

kolossus

You're not creating anything here, the container is. All you've done is declare a dependency to be injected, this is how DI works in JavaEE. A very simplistic view of what is happening:

  • Your Movies EJB is proxied by the container

  • The proxy introspects your class and discovers the annotation you've declared, along with the field you've declared it on

  • The container provides an instance of the EntityManager to the proxy, which in turn makes it available to your implementation.

Presto: Instant EntityManager. Notice how little you're involved in the process?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Calling methods on interface pointers in Go

From Java

Java: Calling in main implemented Interface Methods from a jUnit - Test

From Dev

Java - Given Interface with methods - how are parameters passed?

From Dev

Java - How to avoid static and default methods in interface

From Java

Abstract methods in a Java interface

From Java

Conflicting interface methods in Java

From Java

Optional Methods in Java Interface

From Java

Optional Methods in Java Interface

From Dev

overriding methods in Interface java

From Dev

How Comparator interface is working and how calling the compare() method internally

From Dev

How to call these interface methods?

From Java

Interface methods with variable argument types

From Java

How the functional interface working in java 8

From Dev

Avoiding downcasting when calling interface methods

From Dev

What is the use of calling Interface methods in the implemented class?

From Dev

Struggling with calling overloaded methods from interface to class

From Java

calling interface function that is not implemented in java

From Dev

Java - Passing variable to an interface

From Dev

Java - interface methods naming convention

From Java

Implementing subtypes for interface methods Java

From Dev

Java Interface Design - Helper Methods

From Java

Calling an interface from an interface

From Java

Calling an interface from an interface

From

Type interface {} is interface with no methods

From Dev

How to implement default methods for an interface?

From

What is the difference in calling an interface methods and struct methods in serveHTTP handler?

From Dev

How to call a list of pre-defined interface methods on objects in Java?

From Java

in java, how to make default methods of a interface can not be overridded?

From Java

How to use EntityManager instance in interface using default methods of Java 8?

Related Related

  1. 1

    Calling methods on interface pointers in Go

  2. 2

    Java: Calling in main implemented Interface Methods from a jUnit - Test

  3. 3

    Java - Given Interface with methods - how are parameters passed?

  4. 4

    Java - How to avoid static and default methods in interface

  5. 5

    Abstract methods in a Java interface

  6. 6

    Conflicting interface methods in Java

  7. 7

    Optional Methods in Java Interface

  8. 8

    Optional Methods in Java Interface

  9. 9

    overriding methods in Interface java

  10. 10

    How Comparator interface is working and how calling the compare() method internally

  11. 11

    How to call these interface methods?

  12. 12

    Interface methods with variable argument types

  13. 13

    How the functional interface working in java 8

  14. 14

    Avoiding downcasting when calling interface methods

  15. 15

    What is the use of calling Interface methods in the implemented class?

  16. 16

    Struggling with calling overloaded methods from interface to class

  17. 17

    calling interface function that is not implemented in java

  18. 18

    Java - Passing variable to an interface

  19. 19

    Java - interface methods naming convention

  20. 20

    Implementing subtypes for interface methods Java

  21. 21

    Java Interface Design - Helper Methods

  22. 22

    Calling an interface from an interface

  23. 23

    Calling an interface from an interface

  24. 24

    Type interface {} is interface with no methods

  25. 25

    How to implement default methods for an interface?

  26. 26

    What is the difference in calling an interface methods and struct methods in serveHTTP handler?

  27. 27

    How to call a list of pre-defined interface methods on objects in Java?

  28. 28

    in java, how to make default methods of a interface can not be overridded?

  29. 29

    How to use EntityManager instance in interface using default methods of Java 8?

HotTag

Archive