jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Using interface reference variable without instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using interface reference variable without instantiation" Watch "Using interface reference variable without instantiation" New topic

Using interface reference variable without instantiation

Fei Wong

Joined: Jun 15, 2009
Posts: 11
Hi all, I have a question on the servlet. The code is in below

May I know why the HttpServletResponse interface reference variable can straight using the method without instantiation? Thanks
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

Servlets are controlled by the container. The container handles creating request and response objects. The container creates a thread for each request and calls the service method; passing request and response objects as arguments. Go to Sun's site and read about the servlet life cycle. If you have questions on passing objects to methods, please google it.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
To say this differently, the container created a response object which implements the HttpServletResponse interface. All your code has to know is how to call the methods described by the interface. You don't need to know anything about how the container actually implemented them.
Jinglong Wang

Joined: Mar 05, 2010
Posts: 12
Just like this..
I have a interface..

And then, I create a class implement InterfaceDemo interface.

last i test it.


we can see,the console print two lines.

this is just like the servlet, when the containner startup, it can create the servlet instance, than run the HttpServlet service method, it didn't know what the implements is.Just run the interface methods...
I agree. Here's the link: http://aspose.com/file-tools
subject: Using interface reference variable without instantiation
It's not a secret anymore!