Question about Servlet example?

Posted on 2006-04-15
Medium Priority
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

radarsh earned 180 total points
ID: 16461194
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

ID: 16461434
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?


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
With more and more companies allowing their employees to work remotely, it begs the question: What are some of the security risks involved with remote employees and what actions should we take to secure them?
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 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