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: 3011
9
posted
0
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
posted
0
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: 47
posted
0
Servlets are also intefaces rt? then how we have constructors for that? confusing...
Nicola Garofalo
Ranch Hand
Joined: Apr 10, 2010
Posts: 308
posted
0
When you write a Servlet, you are writing a class that implements Servlet interface (directly or indirectly extending the abstract classHttpServlet 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.
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
posted
0
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: 47
posted
0
Thanks to everyone
Harry Tuttle
Greenhorn
Joined: Apr 08, 2010
Posts: 24
posted
0
karthik manick wrote:Please explain why?
interfaces cannot be instantiate so you don't need any constructor.