This is a sort of best practice. Its called "code to interface". Basically whenever you use such an API,
you should know only the contract of the API i.e. what is the role of which interface and what each method does. If you use an interface, you'll now know which class' instance you are getting. So if the underlying class changes your code would not be impacted at all. I'll take an example of HttpServletRequest interface. When you use it in your
servlet, you don't know instance of which class you are getting.
Tomcat returns you an instance of a different class than Glassfish. The interface is basically the contract defined in Servlet specification which is implemented by Tomcat and Glassfish with their own classes...