Hello Sunil, It is not pretty but you can:
Object o = list.get(0);
if (o instanceof Child) {
((Child)(o)).childMethod();
} else if (o instanceof Parent) {
((Parent)(o)).parentMethod();
}
Or you can have a Enum in the parent that will store the type and in
that case you can just switch over it.
sunil kumar wrote:
> Hi Gustavo,
>
> Thank you for your reply.
> My problem is that, I have different subclasses of the Class Question. and
> since the form is dynamic, there is no way for me to know which Question
> objects are going to come through...
> I thought the only way is to have an interceptor or i just read about type
> converters.. using which i can determine the correct class before the objects
> reach my action class..
>
> --- On Wed, 22/4/09, Gustavo Felisberto <[email protected]>
> wrote:
>
> From: Gustavo Felisberto <[email protected]>
> Subject: Re: How to get subclass objects in an arraylist?
> To: "Struts Users Mailing List" <[email protected]>
> Date: Wednesday, 22 April, 2009, 2:25 PM
>
> I guess the problem IS the fact that the type on elements in the list
> not being the correct type.
>
> public class Parent {
> private String parent = "parent";
>
> public String getParent() {
> return parent;
> }
> public void setParent(String parent) {
> this.parent = parent;
> }
> }
>
> public class Child extends Parent{
> private String childName = "child";
> public String getChildName() {
> return childName;
> }
> public void setChildName(String childName) {
> this.childName = childName;
> }
> }
>
> If you do:
>
> List<Parent> list = new ArrayList<Parent>();
> list.add(new Child());
> list.get(0).getChildName();
>
> It will not build, you have to cast the Object to the correct type:
> ((Child)list.get(0)).getChildName();
>
>
> Same thing for jsp files.
>
>
--
Gustavo Felisberto.
WIT-Software, Lda
Coimbra (Portugal), San Jose (California)
Phone: +351239801030
Web: http://www.wit-software.com
signature.asc
Description: OpenPGP digital signature

