Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on "assigning Object to Context interface"

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

While I was going through the Online tutorial on J2EE 1.4, I found the following piece of code, which confused me.

Create an initial naming context.
Context initial = new InitialContext();
Context myEnv = (Context)initial.lookup("java:comp/env");
The java:comp/env name is bound to the environment naming context of the ConverterClient component.


My question here is , initial.lookup() method would return an object of type Object class. How could we make a reference variable of type "Context" interface to refer the object of type "Object". I haven't seen anywhere Object implementing Context interface. What kind of polymorphism is that? Or is that anything else. I'm very new to java/J2EE area and hence I'm not sure if I had mistaken that code.

Thanks in advance.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an addition to the above question.
How do we actually bind the bean to the JNDI. I didn't see any code like ctx.bind(Object,String)? Where do we set this binding?
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I haven't seen anywhere Object implementing Context interface.

It's Context that extends Object and not Object implements Context. Every class in Java extends Object (implicitly).


It's actually vendor specific. Since you mentioned about J2EE 1.4 Online Tutorial, you can use deploytool to do that. First, open the EJB JAR, then click on Sun-specific Settings. Map your bean with your JDNI name there.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Freddy,

Yes I accept that every thing in java extends from Object. Since I believed that, I was not able to understand assigning an object type of Object to a context. Please look below piece of code

Context initial = new InitialContext();
Context myEnv = (Context)initial.lookup("java:comp/env");

when we execute lookup() method, we are sure it returns an object of type Object. How can we make myEnv which is a Context to refer. Also don't we get java.lang.ClassCast Exception when we invoke any method using myEnv.

Also the same query extends to
Assume we have an Object Dog, with method wag() and if we say

Dog d=(Dog)req.getAttribute(); //req is a HttpServletRequest
d.wag(); //don't we get java.lang.ClassCastException.

Please clarify on this.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand more, take a look at this simple example.

Assigning o to a String is legal in Java because String extends Object. Then to get back the String, you cast the o to String again.

Inside the HttpServletRequest.getAttribute() API, it does something like this:

Then to get the Dog object back, you cast it to Dog.


In Java, this is called downcasting. Hopefully it clears your doubt.

[ July 07, 2007: Message edited by: Freddy Wong ]
[ July 07, 2007: Message edited by: Freddy Wong ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Freddy,

Its clearly understood now. I was thinking getAttribute() would return something like {return new Object();} thats why I was confused. Thanks for explaining me the things here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic