Login  Register

Re: inheritance

Posted by dscho on Aug 08, 2006; 11:43pm
URL: http://imagej.273.s1.nabble.com/inheritance-tp3701842p3701845.html

Hi,

On Tue, 8 Aug 2006, Tony Shepherd wrote:

> ImagePlus imp = WindowManager.getCurrentImage();
> MyTypeOfImage MyImage = imp.getProcessor();

You need to cast it, since MyTypeOfImage is _not_ a base class of
ImageProcessor, but vice versa:

        MyTypeOfImage MyImage = (MyTypeOfImage)imp.getProcessor();

But this _will_ fail if the image processor of the ImagePlus is not of the
correct type, with a ClassCastException.

If you want to access members of ImageProcessor on an instance of, say,
ColorProcessor, you would need to inherit from ColorProcessor, and use
static methods (since it is not possible to "enlarge" a ColorProcessor to
something derived from ColorProcessor, even if it does not contain extra
data members).

For example:

        public class XYT extends ColorProcessor {
                private XZY() { } // we never instantiate this class

                public static int[] getSnapshotPixels(ColorProcessor p) {
                        return p.snapshotPixels;
                }
        }

(I did not actually test that code, but it should compile.)

Hth,
Dscho