File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JspContext to PageContext cast

 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
We all know that an object of a superclass cannot be casted to an object of a subclass.Now here we get the deviation from this rule

(Just to be clear,PageContext is a subclass of JspContext.Actually,it's a interface Hierarchy.Check the following link
Java Class Hierarchy)

public void doTag()throws JspException,IOException
{
JspWriter out=getJspContext().getOut();
out.println("Tag gets going");
PageContext context=(PageContext)getJspContext(); //Here is a typecast from JspContext to PageContext
context.getRequest();
getJspContext().setAttribute("names", "here they are");
getJspBody().invoke(null);

}
More formally,I am using reference variable of type pagecontext for JspContext object returned by getJspContext() method.I am not getting ClassCastException.

Can anyone tell me how this happened???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "deviation from the rule".

What is being returned from the method is a PageContext instance.

Remember that it is possible to refer to an extended class by one of its ancestors. In this case, the method could return any class that extends JspContext. In this case, it's a PageContext.
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There is no "deviation from the rule".

What is being returned from the method is a PageContext instance.

Remember that it is possible to refer to an extended class by one of its ancestors. In this case, the method could return any class that extends JspContext. In this case, it's a PageContext.


but when we see the return type of getJspContext() it returns JspContext and if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.
Then why do we cast it?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapnil Dharane wrote: if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.

Incorrect. This is just basic Java. If you have a reference to a subtype of an instance, you need to cast it to use it as the extended type.
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Swapnil Dharane wrote: if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.

Incorrect. This is just basic Java. If you have a reference to a subtype of an instance, you need to cast it to use it as the extended type.


All right.So you want to say that getJspContext() method returns a PageContext instance.But we still need to cast it

Can you explain this with an example related to PageContext and JspContext?
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapnil Dharane wrote:
Bear Bibeault wrote:
Swapnil Dharane wrote: if getJspContext() was to return pageContext instance,there would have been no need to cast the result of getJspContext() method to PageContext.

Incorrect. This is just basic Java. If you have a reference to a subtype of an instance, you need to cast it to use it as the extended type.


All right.So you want to say that getJspContext() method returns a PageContext instance.But we still need to cast it

Can you explain this with an example related to PageContext and JspContext?

As we can cast JspContext to PageContext,I can see only two possibilities.Correct me if I am wrong
1)PageContext is not exactly a subclass of JspContext.If it was a subclass of jspContext we could not cast JspContext reference to it.
2)PageContext and JspContext have some relationship other than inheritance.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is exactly the same as a method that stays it returns on Object, but actually returns a String. When you get the instance back, you need to cast it to a String to use it as a String.
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it finally........
But please tell me if I was wrong with the two possibilities I stated in previous post
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both suppositions are wrong.

 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Both suppositions are wrong.



Then correct me please :D .And Please explain How do we describe the relationship with these classes
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)PageContext is not exactly a subclass of JspContext.

It is a subclass of JspContext. It's easy enough to check this sort of thing by referencing the javadoc.

If it was a subclass of jspContext we could not cast JspContext reference to it.

Nonsense. Of course you can. This is basic Java. Consider:

Perfectly legal. As String is a subclass of Object, a reference to a String can be its super class Object. But in order to get at it as a String, it needs to be cast.

Same thing happening here. You have a reference to a PageContext as its JspContext superclass. It's perfectly legal to upcast the superclass reference to the actual subclass.

2)PageContext and JspContext have some relationship other than inheritance.

Nope. See above.
 
Swapnil Dharane
Ranch Hand
Posts: 55
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear..... That means getJspContext() returns JspContext reference which actually refers PageContext instance right?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic