How does EJB support polymorphism?

Daniel Chong

Because an EJB consists of multiple "parts", inheritance is achievable in a rather limited fashion (see FAQ answer on inheritance here). There have been noteworthy suggestions on using multiple inheritance of the remote interface to achieve polymorphism, but the problem of how to share method signatures across whole EJBs remains to be addressed. The following is one solution to achieving polymorphism with Session Beans. It has been tried and tested on WebLogic Apps Server 4.50 with no problems so far.

We will use an example to show how it's done. Say, there are 2 session beans, Tiger and Lion, that share some method signatures but provide different implementations of the methods.

  • AnimalHome and Animal are the home and remote interfaces. The signatures of the polymorphic methods are in Animal.
  • AnimalBean is the base implementation bean.
  • TigerBean and LionBean extend from AnimalBean. They may override the methods of AnimalBean, implementing different behaviors.
  • Deploy Tiger and Lion beans, specifying AnimalHome and Animal as their home and remote interfaces. Note that Tiger and Lion should have different JNDI lookup names.