• 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

How come interfaces can be instantiated?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a newcomer in JavaWorld, but I thought I understood from a book (Head First Java) that interfaces, if not implemented, cannot be instantiated. But I am afraid my understanding was wrong, since, for example, a servlet container can create a ServletRequest object, and ServletRequest is not a class but an interface. How can a ServletRequest be instantiated? Since none of its methods are implemented, how can it do anything?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Francois,
Welcome to JavaRanch!
You were right the first time. Interfaces cannot be instantiated. A servlet container doesn't create ServletRequest objects -- it creates an instance of some class like org.apache.catalina.HttpServletRequestImpl that implements that interface.
Part of writing a servlet container (or a JDBC driver, or any other API that involves interfaces) is providing implementations for all those interfaces.
Get it?
[ October 15, 2003: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface could not be instantiated, but it can hold the instance of a class that implements that interface.
 
reply
    Bookmark Topic Watch Topic
  • New Topic