Wierd problem

S

suneet.taparia

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
 
I

Ingo R. Homann

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()
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?

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
 
P

Paul Cager

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.
... ...
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.

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?
 
L

Luke Webber

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top