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?
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.

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?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.