• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using interface reference variable without instantiation

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just like this..
I have a interface..

And then, I create a class implement InterfaceDemo interface.

last i test it.

Result:


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...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic