this is a question i got from Nikos' blog and it says like this:
Does this servlet compile successfully? (1 correct answer)
1. Compilation succeeds.
2. Compilation fails because there is no init(ServletConfig) in GenericServlet.
3. Compilation fails because init(ServletConfig) of GenericServlet throws ServletException.
4. Compilation fails because init(ServletConfig) of GenericServlet throws IOException and ServletException.
It is said that the answer is 3, but i would say the answer is 1 bacause GenericServlet has a method.
so, please you guys help me by explaining the reason
What is actually happening?
we are overriding the init() method in our sub servlet class, it is a specific class, so we avoid throwing that exception.
When you are overriding a method it is allowed to have less exceptions then your parent, that is correct.
If the compiler invokes the super class init() method, then compiler gives us a compilation error.
Our overriding method is explicitely executing the code of the super constructor (and normally when you want to override you want to add some code in addition to that, but this is just a trick question), so you have to deal with the Exception that might be thrown there.
And if the container directly calls the init() method of our sub class servlet, then no need of catch/declare the exception.
I am not sure what you are saying here