JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Today's Question Monday 5th May 2003

Which of the following method signatures represent a correct way of overriding the doGet method in HttpServlet? (2 correct answers)



A void doGet(HttpServletRequest request, HttpServletResponse response) throws HttpServletException, IOException
B public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
C protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException
D protected boolean doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
E public boolean doGet(HttpServletRequest request, HttpServletResponse response) throws HttpServletException, IOException
F protected void doGet(HttpServletRequest req, HttpSession resp) throws ServletException, IOException
Page down for the answer...











































The Answer

B & C are the correct answers. Each throws only one of the two exception types present in the superclass doGet. But that’s OK – an overriding method can throw fewer exceptions than its parent.
To weed out the imposters, you need to know the following:

  1. The superclass method doGet has an access modifier of “protected”. Since you can’t reduce visibility in an overridden method, the only access modifiers acceptable are “protected” or “public”. This rules out answer A.
  2. The return type for doGet is void, not boolean. This rules out answers D and E.
  3. HttpServletException doesn’t exist (at least, not in the javax.servlet packages as delivered). This rules out A and E (again).
  4. The correct parameter types for doGet are HttpServletRequest and HttpServletResponse. HttpSession is a type that exists, so the F answer will compile – but it doesn’t override the original method (it overloads doGet rather than overriding it).


EMail: dbridgewater@jbridge.co.uk
Phone: +44 (0)1943 877414
Fax: +44 (0)1943 877414
Mail: David Bridgewater, Willow Dene, Bradford Road, Menston, Ilkley, West Yorkshire, LS29 6ED, UK
Copyright © 2003 David Bridgewater. All rights reserved.