• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP polymorphism problem in <jsp:useBean />

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Chapter 8 Scriptless Jsp in book Head First Servlets and Jsp.

In SCJP book by Kathy and Sierra it's mentioned :

class Parent {
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
class Child extends Parent {
private String id;
public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
}

Now if i will try to compile the following code:
Parent p = new Child();
p.setName("Hi Ravi");
p.setId("EID12");
It's giving me error: method setId not found.

Similary i am trying to interprete the <jsp:useBean /> as:

Testing the Polymorphism Concepts.......


<jsp:useBean id="polyper" type="Parent" class="Child" scope="page">
<jsp:setProperty name="polyper" property="name" value="Poly Dude !!!" />
<jsp:setProperty name="polyper" property="id" value="EID34" />
</jsp:useBean>

Getting the Name property


<jsp:getProperty name="polyper" property="name" />
<jsp:getProperty name="polyper" property="Id" />

and i am getting the following output:

Testing the Polymorphism Concepts.......

Getting the Name property
Poly Dude !!! EID34

So could anyone please explain how is it going on. Means using a type as Parent and class as Child and still i am getting the ID in the output without any exception.

Inside Generated JSP:
Parent polyper = null;
polyper = (myBeans.Parent) _jspx_page_context.getAttribute("polyper", PageContext.PAGE_SCOPE);
if (polyper == null){
polyper = new myBeans.Child();


out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((myBeans.Child)_jspx_page_context.findAttribute("polyper")).getId())));

How is it possilbe to acceess the ID attribute by using the type as a super class ?


Hopefully i will get the answer.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhagwat Chouhan wrote:
It's giving me error: method setId not found.


it is clear. your Parent class doent have setId method .

Bhagwat Chouhan wrote:

How is it possilbe to acceess the ID attribute by using the type as a super class ?



you cant.use Child obj = new Child() insted of Parent obj = new Child()

Hope this helps
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way:use code tag to post your code . And Welcome to Javaranch
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't use supercalss reference to call child method.when you use pholimorphic reference compile time it checks the parent class.then there is no such a method call setId.then it doesn't compile.
 
Bhagwat Chouhan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here my doubt is that how the code is working fine in tomcat when i used Parent as type and Child as class. Means i am able to set and get the values of the Child attributes not listed in Parent attributes. This should not be possible, but it's working fine.
 
Sheriff
Posts: 67746
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
Look at the servlet generated by Tomcat on behalf of the JSp to see how the the useBean action references the object instance.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's giving me error: method setId not found.



That is because setID is in the child class. Though you created an instance of a child through the parent, the child is still seperate. You will have to typecast it : (Child)p.setId

Refer to the below site for detailed explanation of Parent/Child relationship and how inheritance works.
http://math.hws.edu/javanotes/c5/s5.html

Hope this helps,
Juliana
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juliana Steven wrote: Though you created an instance of a child through the parent, the child is still seperate. You will have to typecast it : (Child)p.setId



Nop. it wont help you sorry .
 
Bhagwat Chouhan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all...

I will try to find my answer.

My question is : How parent has access to child's attribute? I am saying this after looking at the generated servlet code.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Like Bhagwat, I am also quite confused with the polymorphism of the jsp:useBean / setProperty.

I am well aware that you cannot access the child method which were not inherited/overriden if you use the parent class as the reference type.

But in the case of the jsp:useBean, the reference type being used here is the parent class;



but why still the line below still works?



* Updated *

I think i found the answer... referring to the first post and @ the jsp servlet generated code...

The container automatically created a typecast to myBean.Child.
 
Bhagwat Chouhan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To - Honiewelle Flores

Hi,

I am also thinking same king of answer. It might be possible that Tomcat is doing the automatic conversion.

Thanks for your reply.
 
reply
    Bookmark Topic Watch Topic
  • New Topic