aspose file tools*
The moose likes Beginning Java and the fly likes Constructors for Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructors for Interface" Watch "Constructors for Interface" New topic
Author

Constructors for Interface

karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
I read this in a website "Constructor arguments cannot be specified on an interface". Is it so? Please explain why?
Serlets are also interfaces...but i have seen constructors for servlets...contrdicting?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3533
    
  15
The simple answer is - that's the way Java was designed.
You might find a more expansive answer in the Java Language Specification


Joanne
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Interfaces and Constructors?
You shouldn't even think about constructors when you deal with interfaces, regardless of the number of parameters.
Interface is not a class, it does not have constructors.


Bye,
Nicola
karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
Servlets are also intefaces rt? then how we have constructors for that? confusing...
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
When you write a Servlet, you are writing a class that implements Servlet interface (directly or indirectly extending the abstract class HttpServlet that indirectly implements Servlet interface).

The path is:

YourServlet extends HttpServlet that extends GenericServlet that implements Servlet. Thus, YourServlet implements Servlet

Since you are writing a class, you can talk about constructors.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

karthik manick wrote:Servlets are also intefaces rt? then how we have constructors for that? confusing...

Not really: the constructors aren't part of the interface. Classes have constructors. Interfaces don't. *All* classes have constructors (even if it's not explicitly coded by you, there's still a default constructor) regardless of any interfaces it implements.
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
karthik manick wrote:I read this in a website "Constructor arguments cannot be specified on an interface". Is it so? Please explain why?
Serlets are also interfaces...but i have seen constructors for servlets...contrdicting?




List is an interface, and defines a contract (what methods are available in classes that implement it).
ArrayList is a class, it can be instantiated. It has constructors.
Methods can hide creating instances, and return the interface, just like above. That way, they can hide their internal behaviour, which helps you write code that's easier to maintain: you don't have to worry about the type that's used internally, whether it's an ArrayList, a LinkedList, a CopyOnWriteArrayList or SomeSpecialList.

Same with your servlets.
karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
Thanks to everyone
Harry Tuttle
Greenhorn

Joined: Apr 08, 2010
Posts: 24
karthik manick wrote:Please explain why?



interfaces cannot be instantiate so you don't need any constructor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors for Interface