• 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

Question on polymorphic bean references.

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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

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.
 
Scott Updike
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool.

Thanks for your help.
Scott
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic