Hi,
I'm facing a wierd problem in my code:
There's is a servlet that is having a service() and a doPost() in it
and this servlet is extented by a class that also has a doPost()
implemented.
What actually happens is that :
When request reaches my servlet then in the service() then I call
doPost() method but instead of calling the doPost of the servlet its
calling the doPost() of the child.
Can anyone explain why is this happening?
Thanks in advance
Ingo R. Homann - 27 Apr 2006 10:11 GMT
Hi,
> There's is a servlet that is having a service() and a doPost() in it
> and this servlet is extented by a class that also has a doPost()
[quoted text clipped - 7 lines]
>
> Can anyone explain why is this happening?
It's not a bug, it's a feature! That is exactly what is supposed to
happen, it's not weird at all. That's what's OO about.
If you do not want that the method of the "subclass" (not "child") is
called, then you must not "override" it. To avoid (in the "superclass")
that the method can be "override" in a "subclass", you can declare the
method as "final". (Of course, in your case that leads to a compiler
error in the subclass. You have to rename the method in the subclass.)
I strongly suggest to read some OO-Books!
Ciao,
Ingo
suneet.taparia@gmail.com - 28 Apr 2006 06:59 GMT
Thanks man .... I got your point....
Paul Cager - 27 Apr 2006 10:19 GMT
> There's is a servlet that is having a service() and a doPost() in it
> and this servlet is extented by a class that also has a doPost()
[quoted text clipped - 3 lines]
> doPost() method but instead of calling the doPost of the servlet its
> calling the doPost() of the child.
If I've understood you correctly then this would seem to be normal
behaviour - an instance of the dervied class should call its own
(overridden) version of the doPost method. What is it you are trying to
achieve?
suneet.taparia@gmail.com - 28 Apr 2006 06:59 GMT
Thanks man .... I got your point....
Luke Webber - 27 Apr 2006 12:41 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> Can anyone explain why is this happening?
Which class are you calling in your HTML? The child or the parent? As an
example, let's say the parent is called ParentServlet and the child is
called ChildServlet. If you call ParentServlet, you can expect to get
the result you seem to want, but if you call ChildServlet, that would
explain what you're seeing.
Luke