Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors for Interface

 
karthik manick
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer is - that's the way Java was designed.
You might find a more expansive answer in the Java Language Specification
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
karthik manick
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are also intefaces rt? then how we have constructors for that? confusing...
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone
 
Harry Tuttle
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik manick wrote:Please explain why?



interfaces cannot be instantiate so you don't need any constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic