Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

why the caste?

 
Ranch Hand
Posts: 4702
9
Scala Java
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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: 4702
9
Scala Java
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!