• 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

Constructors for Interface

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

karthik manick wrote:Please explain why?




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