if HttpSession is abstarct interface then can we create object of this inte

Hello Everyoneif HttpSession is abstarct interface then can we create object of this interface ?if No , then plz explain HttpSession session = request.geSession() ?
[185 byte] By [Dhruvanga] at [2007-11-27 8:50:09]
# 1

> if HttpSession is abstarct interface then can we

> create object of this interface ?

No. Besides, it's interfaces are always abstract.

> if No , then plz explain HttpSession session =

> request.geSession() ?

Where's an object creation there?

plz explain System.out.println(session.getClass().getName());

And you do know what a reference is, don't you?

CeciNEstPasUnProgrammeura at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 2
> HttpSession session = request.geSession()This line says that session, returned by request.geSession(), is an object that implements the HttpSession interface.
java_knighta at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 3

This a a classic example of the use of interfaces. Sessions are implemented and managed by your servlet container, be it Tomcat, Glassfish, OC4j etc..

The actual ("concrete") class of these objects depends on the server involved, and the class won't be available to you when you compile your servlets. So you write code which doesn't depend on which kind of server you are on.

So, no, your code never creates these objects for itself. The server creates them where it's appropriate and passes them to your code.

malcolmmca at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 4
but , my question is how can we call method using refrence ?
Dhruvanga at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 5
but , my question is how can we call method using refrence ?
Dhruvanga at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 6

> but , my question is how can we call method using

> refrence ?

That's how interfaces work. If you have a reference which the compiler knows contains an object implementing, say, HttpSession, you can call the methods of HttpSession on that reference and the JVM will map them to the real method implementations in the concrete class.

malcolmmca at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 7

> That's how interfaces work. If you have a reference

> which the compiler knows contains an object

> implementing, say, HttpSession, you can call the

> methods of HttpSession on that reference and the JVM

> will map them to the real method implementations in

> the concrete class.

I.e. you do not care if request.geSession() returns an object HttpFooSession, HttpBarSession, HttpBazSession, etc. as long as all these objects implement HttpSession.

java_knighta at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...
# 8

EDIT:

As far as I can tell, these three threads are all the same person asking the same question.

http://forum.java.sun.com/thread.jspa?threadID=5188609

http://forum.java.sun.com/thread.jspa?threadID=5188638

http://forum.java.sun.com/thread.jspa?threadID=5188603

How obnoxious. I retract my help.

Message was edited by:

jverd

jverda at 2007-7-12 21:00:47 > top of Java-index,Java Essentials,Java Programming...