This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

ServletA extend ServletB right or wrong ?

 
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ServletA extend ServletB right or wrong ?

ServletA & ServletB are two separate class extending HttpServlet.
They both have init() method.

some of the variable & methods define in ServletA are also required in ServletB, is it the good idea to for ServletB to extend ServletA and also calling super.init() from ServletB.init() method. in this case ServletA.init() will be called twice. is it right or wrong
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..init of A will be called twice.
 
hasan khan
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i know init() of ServletA will be called twice, but is it right approach to do so. since init() of any servlet instance get executed once and only once.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is some confusion about inheritance here...

If you have two class files (A and B) and B extends A; calling a super.methodName() method in an instance of B will not cause an methods of a separate instance of class A to be called.

In the case of servlets..
If you create two servlet class files (again A, and B) and then deploy two servlets in a container (Let's say AServlet and BServlet, each being an instance of class A, and B respectively); deploying BServlet will not cause AServlet's init method to be called.

In other words, an instance of BServlet is still only one servlet as far as the container knows. The container doesn't know or care that there is a superclass (AServlet) tucked away inside of BServlet.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still not convinced about this.I will try out this sometime this weekend and let you know.

considering servet B extending A

The servlet B is having servlet A in encapsulated form.And again servlet B exists independently also.So the init method of A would be called twice.
 
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:

The servlet B is having servlet A in encapsulated form.



You have a grave misunderstanding regarding how Java works. When a class is extended, there is no "encapsulated" form. That's the difference between containment and inheritence.

If B extends A and B defines an init() method, B's init will be called once and A's will never be called. If B does not implement init, then it will inheret A's and it will be called once.
[ September 15, 2006: Message edited by: Bear Bibeault ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I have tested this in my local tomcat instance.I am finding the init of servlet A getting called twice.I think you have gone through the original thread.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a feeling this would happen and was trying to figure out how to word an explaination that would make it clear.

When you inherit from a class, there is, in effect, an instance of that superclass living inside instances of the subclass. This is why any print statements inside the superclass's init method were called when the subclass (servlet b) was loaded.

This is entirely different from the OTHER instance of servlet A that is running in your container.


Another way to look at it.
With the given scenerio, you (in effect) had 3 servlet instances running.
1.) Servlet A (created from class A).
2.) Servlet B (created from class B).
3.) A secret copy of servlet A (call it SuperA).

The third one (the superclass to servlet B) lives inside of servlet B and is not visible to the servlet container. Only servlet B knows about it.

The init method to Servlet A was only called once.
The other print statement that you saw was Servlet B calling it's (secret) superclass; not Servlet A's.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic