aspose file tools*
The moose likes JSP and the fly likes Question on polymorphic bean references. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Question on polymorphic bean references." Watch "Question on polymorphic bean references." New topic
Author

Question on polymorphic bean references.

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
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?
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

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 ]
Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
Cool.

Thanks for your help.
Scott
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on polymorphic bean references.