Question about Servlet example?

Posted on 2006-04-15
Last Modified: 2010-05-18

My wrox java server pages text, includes an example where some user data is posted to servlet.

To process the data posted and store this, they created a interface class and another class AdminManager which implements the interface.

In the servlet they created an object from the interface class, and called the AdminManager methods through the interface object.

In the book this was the only example which used a interface. And I wondered if this design approach was often used, or should an interface normally be used if several classes would implement the interface.

Question by:AntoniRyszard
    LVL 10

    Accepted Solution

    Yes you should use the interface approach wherever applicable. It makes
    sure your calling code doesn't get affected by your called code.

    >> they created an object from the interface class
    You can put that as "they used the interface as a reference"

    Well, for example,

    List list1 = new ArrayList();
    List list2 = new LinkedList();

    now, list1.add() will actually invoke the ArrayList's add method
    and list2.add() will invoke the LinkedList's method.

    This is called Runtime Polymorphism.


    Author Comment

    Thank you,

    Using the interface is a good java design practice?

    I was thinking we could alternatively, write a class which includes methods to process and store the servlet data. And we call the constructor of this class in the servlet. And the classes other methods through the constructor.

    Have you possibly seen any examples where the use an interface class, a class which implemented the interface and a servlet was used?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now