• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

servlet's init method

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the kind of question which you could see at the SCJP exam... HttpServlet#init(ServletConfig config) throws a checked exception, ServletException. What's happening to it, if you're neither catching it, nor rethrowing it ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've a doubt about this, here, we are overriding the init() method in our sub servlet class, it is a specific class, so we avoid throwing that exception. But, If the compiler invokes the super class init() method, then compiler gives us a compilation error. Is this the real case? And if the container directly calls the init() method of our sub class servlet, then no need of catch/declare the exception.

What is actually happening?
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abimaran,

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

Regards,
Frits
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget about servlets for a moment. Here is a basic Java example to illustrate the problem.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Christophe! Now I got it!
 
tehika danait
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is now clear, thanks. i agree that this is more like an scjp than scwcd
 
That new kid is a freak. Show him this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic