This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How come interfaces can be instantiated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How come interfaces can be instantiated?" Watch "How come interfaces can be instantiated?" New topic
Author

How come interfaces can be instantiated?

Francois Yergeau
Greenhorn

Joined: Oct 15, 2003
Posts: 3
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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 ]

[Jess in Action][AskingGoodQuestions]
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
An interface could not be instantiated, but it can hold the instance of a class that implements that interface.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How come interfaces can be instantiated?
 
Similar Threads
Doubt about ServletRequest Interface
doFilter() method parameters
How to get the parameters of servletRequest
Difference between object and instance
get the remote IP address from a LoginModule