aspose file tools*
The moose likes JSP and the fly likes Why simple tags can have Classic parents? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Why simple tags can have Classic parents?" Watch "Why simple tags can have Classic parents?" New topic
Author

Why simple tags can have Classic parents?

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
<my:ClassicParent>
<my:SimpleChild />
</my:classicParent>

ClassicParent is a Classic Tag.
SimpleChild is a Simple Tag.

I think as follow.
ClassicParent aTag = (ClassicParent)getParent();
But getParent return type is JspTag.
ClassicParent's type is Tag.
This will cause a cast exception.
But why does it say "simple tags can have Classic parents" in book?


My Way,My Pace
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

Look at the class hierarchy for javax.servlet.jsp.tagext again. What do you notice?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
Originally posted by Bear Bibeault:
Look at the class hierarchy for javax.servlet.jsp.tagext again. What do you notice?


ClassicTag extends TagSupport that implements IterationTag and Tag interface.
Simpletag extends SimpleTagSupport that implements SimpleTag interface.
They body share the JspTag super interface.
JspTag-Tag
........-SimpleTag

but Classictag's getParent method return type is Tag.
SimpleTag's getParent mehod return type is JspTag.

I don't think parent can convert to child(in other word,I think JspTag can't cast to Tag)
is my cast concept wrong?

[ July 16, 2005: Message edited by: avseq anthoy ]
[ July 16, 2005: Message edited by: avseq anthoy ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

is my cast concept wrong?


Yes, you can upcast as long as the object you are upcasting is indeed an implementation of the type to which you are casting.

So, if the object is a Tag but you are returned a JspTag reference, it's perfectly OK to upcast it to Tag.

If you were not able to do this, the (non-generics) collection classes would be useless.
[ July 16, 2005: Message edited by: Bear Bibeault ]
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104

above couse excption.


above work fine.

So,why
Tag t = (Tag)JspTag. Can work?
[ July 16, 2005: Message edited by: avseq anthoy ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

Once again, it all depends on what type the object actually is. The JspTag reference returned can actually be any one of the types that implements JspTag. So it could be a Tag, or a BodyTag, or even SimpleTag.

If you are returned an object that is a SimpleTag and upcast it to Tag, you will get a run-time ClassCastException. But if that object is indeed an instance of Tag, the upcast will work.
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
Originally posted by Bear Bibeault:
Once again, it all depends on what type the object actually is. The JspTag reference returned can actually be any one of the types that implements JspTag. So it could be a Tag, or a BodyTag, or even SimpleTag.

If you are returned an object that is a SimpleTag and upcast it to Tag, you will get a run-time ClassCastException. But if that object is indeed an instance of Tag, the upcast will work.


I agree the second paragraph.
But about the first paragraph.
I think Tag a type implementing JspTag;
So JspTag tag2 = (Tag)tag1;-->Tag1 is a Tag.this is true,because Tag is a type of JspTag;
But why
Tag tag2 = Tag(tag1)-->Tag1 is a JspTag.This mean JspTag is a type of Tag,that's wrong.

I was crazy..........

[ July 16, 2005: Message edited by: avseq anthoy ]
[ July 16, 2005: Message edited by: avseq anthoy ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

Tag tag2 = Tag(tag1)-->Tag1 is a JspTag.This mean JspTag is a type of Tag,that's wrong.


Correct, if tag1 is not an implementation of Tag, this will cause a ClassCastException.

In your original example, the getParent method will never return an instance of an object that is just an implementation of JspTag. It will be one of SimpleTag, Tag, BodyTag or IterationTag, all of which are implementations of JspTag.
[ July 16, 2005: Message edited by: Bear Bibeault ]
avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
Oh oh!I got it!
Thx to Bear Bibeault!!!
It seens that I should imporve my basic concept of java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why simple tags can have Classic parents?