Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1370

Re: inheriting BufferedImage and using ImageIO.read()

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!novia!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: inheriting BufferedImage and using ImageIO.read()
Date Sun, 20 Nov 2011 08:11:22 -0800 (PST)
Organization http://groups.google.com
Lines 51
Message-ID <15176118.252.1321805482227.JavaMail.geo-discussion-forums@prdy11> (permalink)
References <4ec8ebc3$0$8437$426a34cc@news.free.fr>
Reply-To comp.lang.java.help@googlegroups.com
NNTP-Posting-Host 173.164.137.214
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1321806000 30474 127.0.0.1 (20 Nov 2011 16:20:00 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sun, 20 Nov 2011 16:20:00 +0000 (UTC)
In-Reply-To <4ec8ebc3$0$8437$426a34cc@news.free.fr>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1370

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar


Thread

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