Question about Servlet example?


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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AntoniRyszardAuthor Commented:
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?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.