Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Absolutely new to EJB.. Regarding implementing methods in interfaces..

Posted on 2004-10-27
4
Medium Priority
?
308 Views
Last Modified: 2013-11-24
Hi all.. Please answer this asap..
I have ".java" files in a package like this:

---------------------------------------------------------------------------
Authentication.java:-

package com.it.foundation.signon;
import javax.ejb.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
import com.it.foundation.*;
public interface Authentication extends EJBObject, BeanInterface{
}
---------------------------------------------------------------------------
AuthenticationHome.java:-

package com.it.foundation.signon;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface AuthenticationHome extends EJBHome {
      public Authentication create() throws RemoteException, CreateException;
}
---------------------------------------------------------------------------
The "BeanInterface.java" which is used in the "Authentication.java" is as given below:

package com.it.foundation;
import java.rmi.RemoteException;
import com.it.foundation.action.CommandRequest;
import com.it.foundation.action.CommandResponse;
public interface BeanInterface
{
          public CommandResponse execute(CommandRequest request) throws RemoteException;
}
---------------------------------------------------------------------------
**********************************************
Note that both the files above contain interfaces.
**********************************************

Now I have a file by the name "EJBGetter.java":

package com.it.foundation.utils;

import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.it.foundation.signon.AuthenticationHome;

public final class EJBGetter {
    private static InitialContext initial = null;

    public static InitialContext getContext() throws NamingException {
        if (initial == null) {
            initial = new InitialContext();
        }

        return initial;
    }

    public static AuthenticationHome getAuthenticationHome() throws NamingException {
        InitialContext context = getContext();
        Object objref = context.lookup(CConst.AUTHENTICATION_EJBHOME);

        return (AuthenticationHome) PortableRemoteObject.narrow(objref, AuthenticationHome.class);
    }

}
---------------------------------------------------------------------------
**********************************************
QUESTION : How is it possible that the EJBGetter class is creating a function "getAuthenticationHome()" of the interface "AuthenticationHome"?  
**********************************************
---------------------------------------------------------------------------

It is even being used in another servlet as follows :

static {
        try {
            AuthenticationHome home = EJBGetter.getAuthenticationHome();
            authenticationBean = home.create();
        } catch (RemoteException rex) {
            rex.printStackTrace();
        } catch (CreateException cex) {
            cex.printStackTrace();
        } catch (NamingException nex) {
            nex.printStackTrace();
        }
    }
-----------------------------------------------------------------------------------------------
************************************************************
How is this possible? Could someone please explain. I have this code working correctly. I think I am missing somerthing here. But isn't an interface NOT allowed to implement code in its method. Moreover how is another class implementing the body of the method of an interface that it does implement?
0
Comment
Question by:deshani
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12419037
> But isn't an interface NOT allowed to implement code in its method.

EJBGetter is not an interface, it is a class

> Moreover how is another class implementing the body of the method of an interface that it does implement?

A class can create methods of any sig, the fact that the sig is used by an interface is irrelevant.
The signature of that method is actually a littl different, in that it is a static class method.
0
 

Author Comment

by:deshani
ID: 12419123
Hi objects,
"The signature of that method is actually a littl different, in that it is a static class method."
---Does this mean that you are talking about this particular case or in a general sense?
---Does a method with a signature of another class/interface mean that when we use that method we can instantiate the object of that class, as is done in this case? See the EJBGetter class, where we say:
             ------------------------------------------------------
       AuthenticationHome home = EJBGetter.getAuthenticationHome();
             ------------------------------------------------------
Does this not amount to actually creating an object "home" of the interface "AuthenticationHome"?

Thanks,
Anup
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12419158
am talking about this particular case.

yes, EJBGetter.getAuthenticationHome() will return an object of a class that does implement AuthenticationHome
0
 

Author Comment

by:deshani
ID: 12419210
thanks a million...this clarifies it a lot. Still have some doubts, but I believe they are due to lack of knowledge in EJBs. One last request: Could you tell me a few resources that can point to the concept of "creating methods with signature of a different class" and on any reference guide to EJB?
Thanks again.
Regards,
Anup
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question