• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

relation is required B/W class and type before <useBean>

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following java code:



Just to share this knowledge .....


Detailed Explanation

Concept: Whenever you see class and type attribute both in the use bean tag, visualize the tag like this:
typevalue obj = (typevalue) new classValue;
And see, if it is valid at compiletime and runtime.


Here, applying the same logic we get:
MusicCD obj = (MusicCD) new Book();
It is clear that you cannot cast a Book object to MusicCD object because MusicCD does not extend from Book. So this is a compile time error.

so relation is required between class amd type attribute before the use of <useBean> tag in JSP.


 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. It will not compile is the correct answer
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for you aclaration.

But I dont think that the translation will be:

MusicCD obj = (MusicCD) new Book();

I think, that this will be:

MusicCD obj = new Book(); //without the casting.

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope Milton , this is not the option. Polymorphisam will be applied here.
MusicCD obj = (MusicCD) new Book();

This way you can call the MusicCD methods as well.
 
Chithra Salam
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This way you can call the MusicCD methods as well.
-->How is it possible?The code wont get compiled right?


Chithra Salam
SCJP -85%,SCWCD-in progress.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code, will compile

the problem is when the JSP is translating to a Servlet <- this wont compile, for the problems of pholymorph
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is very nicely explained here: http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic