Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1370
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: inheriting BufferedImage and using ImageIO.read() |
| Date | 2011-11-20 08:11 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <15176118.252.1321805482227.JavaMail.geo-discussion-forums@prdy11> (permalink) |
| References | <4ec8ebc3$0$8437$426a34cc@news.free.fr> |
mx wrote: > So, I have a SubImage class which extends BufferedImage and I would like > to use ImageIO.read() to create a SubImage, but since it returns a > BufferedImage, I can't find a way of doing that. > Should I be using another design? or is there a way to get past this > problem? > I would also add that right now my SubImage simply adds methods to the > BufferedImage class. Yet downcasting causes an exception. Since you absolutely show no code whatsoever, much less a Simple, Self-Contained Compilable Example (SSCCE http://sscce.org/), I'll guess, but the lack of information you provide may cause me to miss the mark. (Follow the advice in http://sscce.org/.) Subclassing 'BufferedImage' is almost certainly a mistake, but without knowing your reasons or what changes you made it's hard to be 100% certain. Still, I'd take 10:1 that you should "favor composition over inheritance". (http://java.sun.com/docs/books/effective/) That is, use a class that has a member reference to a 'BufferedImage' rather than extending it. As for the downcast, I'll take 100:1 odds that you created a 'BufferedImage' that was not a 'SubImage' then tried to downcast it. You can only downcast a reference if it points to an instance of the subtype. Otherwise you get a 'ClassCastException'. You did get a 'ClassCastException', right? You didn't even bother to tell us that much, much less what the message said or what code it applied to. To answer your questions: Yes, you should be using another design. Maybe even if you give us more information we could help you figure out what that other design should be. Like, what are you trying to accomplish, and why did you imaging subclassing 'BufferedImage' would help, and *WHAT IS YOUR CODE?* (http://sscce.org/) (Note the "Self-Contained" in that initialism's expansion!) The way to get around your problems (there are more than one), read the Oracle tutorials about inheritance and casting in Java. Buy and study (repeatedly) the book /Effective Java/, 2nd ed., by Joshua Bloch, referenced above. Somebody have the FAQ for asking smart questions handy? Between that and http://sscce.org/ we should be able to help this guy if he follows that advice. So: Don't extend 'BufferedImage'. Don't try to downcast unless you know the instance is of the proper subtype. Do tell us what you want to accomplish, rather than merely how you imagined you should accomplish it. Do study diligently the tutorials, /Effective Java/ and http://sscce.org/. -- Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
inheriting BufferedImage and using ImageIO.read() mx <mx@nomail.com> - 2011-11-20 12:57 +0100
Re: inheriting BufferedImage and using ImageIO.read() Lew <lewbloch@gmail.com> - 2011-11-20 08:11 -0800
Re: inheriting BufferedImage and using ImageIO.read() "John B. Matthews" <nospam@nospam.invalid> - 2011-11-20 15:27 -0500
Re: inheriting BufferedImage and using ImageIO.read() Roedy Green <see_website@mindprod.com.invalid> - 2011-11-20 12:41 -0800
Re: inheriting BufferedImage and using ImageIO.read() markspace <-@.> - 2011-11-20 13:02 -0800
csiph-web