• Post Reply Bookmark Topic Watch Topic
  • New Topic

why the caste?  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4589
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have other questions about this code from oracle as well(like why they used an AbstractDocument in the first place? and why the if statement?), but this question is about the caste(you can answer the others if you like ).

why
doc = (AbstractDocument)styledDoc;
wouldn't
doc = styledDoc;
do the same thing?
am i missing something?

ps. it is from the tutorial on using text components. the file is TextComponentDemo.java.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every StyledDocument isn't an AbstractDocument. StyledDocument doesn't have a method setDocumentFilter(...) but AbstractDocunent does. You can't invoke that method without casting.

Oh, and it's cast not caste.
 
Randall Twede
Ranch Hand
Posts: 4589
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh...and they do call that method. but in my code i don't. i will go look at the API again. i thought StyledDocument extends AbstractDocument(and maybe it does).

i am starting to understand a little better now. StyledDocument is an interface that extends the interface Document.
AbstractDocument is a class that implement Document.

i will get to the bottom of my problem yet. it has something to do with this. first i am going to factor out all the menu stuff to make my main class more readable.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!