Forums Register Login

Question on polymorphic bean references.

+Pie Number of slices to send: Send
Assume I have the following JSP code

-------------------
<jsp:useBean id="person" type="Person" class="Employee">
<jsp:setProperty name="person" property="*" />
</jsp:useBean>
-------------------

that is called from this HTML page

-------------------
<html><body>
<form action="TestBean.jsp">
name: <input type="text" name="name">
ID#: <input type="text" name="empID">
<input type="submit">
</body></html>
-------------------

where Person is an abstract class (with private name property and standard getName, setName methods) and Employee is a concrete subclass of Person(with private empID property and standard getEmpID, setEmpID methods).

When the Container builds the resulting servlet for this JSP, then a bean called "person" is either selected or created (if doesn't exist in page context) that has an object type of Employee that has a reference type Person.

Person person = new Employee();

Now polymorphically speacking, "person" should not be able to call the setter method in Employee (can only call methods in Person), but yet, the container can set both properties. Doesn't this violate the polymorphism rule in plain java?
+Pie Number of slices to send: Send
Possibly.
But the jsp:setProperty tag uses reflection to get/set properties.
As such, it bypasses the rules of java types. It sees that the object actually IS of type Employee, and has a property called empID.

If you attempted to use this class in scriptlet code, then the rules would be applied. ie <%= person.getName() %> would work, but <%= person.getEmpID() %> would not.
+Pie Number of slices to send: Send
 

Originally posted by Scott Updike:

Now polymorphically speacking, "person" should not be able to call the setter method in Employee (can only call methods in Person)



I think you need to review how polymorphism works in Java. The type of the reference does not dictate which method will be invoked.
+Pie Number of slices to send: Send
So if I write

Object test = new Employee();

then

test.setEmpID(1234);

is valid?

The object "test" of type Object has no idea what an Employee is. Or am I wrong?
+Pie Number of slices to send: Send
From what you said, I thought you might have had some confusion about the following. In:


Person person = new Employee();



If Person has a getXyz() method and Employee has a getXyz() method, which will be called by:


person.getXyz()



?

But it comes down to Stefan's point. Since reflection is used by the standard actions, it will pick up the appropriate method.
[ March 06, 2006: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
Cool.

Thanks for your help.
Scott
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, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 980 times.
Similar Threads
jsp:useBean doubt
Error on basic jsp...
Concrete class problem in EL
useBean doubt
EL and jsp:usebean..type/class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:43:26.