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.