This discussion is archived
5 Replies Latest reply: Jun 16, 2013 9:07 AM by rp0428 RSS

Serious Question pertaining to interfaces.

Zachary1234 Newbie
Currently Being Moderated

I want to find a way for my synthetic class to be both a BufferedImage
and a Rectangle 3D
, using what I believe is called "extreme coupling". The following is additionally important because, if it works, it would allow be to convert the workings of a key class into an interface, and then set up; since any number of interfaces may be implemented at one time.
 
-Why will the following not compile on my java 1.6?
//---------------------------------------------------------------------------------
import static java.lang.System.out;
import java.awt.geom.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
 
public class Test
{
 
public static void main(String ... args)
{
try
{
File file = new File("C:\\Users\\User\\Desktop\\krypton.jpg");
 
BufferedImage image = ImageIO.read(file);
 
Raster raster = image.getData();
 
BufferedRectangle quad = new BufferedRectangle();
 
quad.setData(raster); //Error Here. ???
 
out.println(quad);
}
 
catch (Exception e)
{
e.printStackTrace();
}
}
}
 
class BufferedRectangle implements SubRectangle2D
{
}
 
interface SubRectangle2D
{
abstract class Rectangle2DImplementation extends Rectangle2D

                                                                    implements SubRectangle2D

{
}
}

//---------------------------------------------------------------------------------

  • 1. Re: Serious Question pertaining to interfaces.
    jwenting Journeyer
    Currently Being Moderated

    Neither BufferedImage nor Rectangle2D are interfaces, and Java has no multiple inheritance of classes, so you're not going to get that.

     

    What you can do is create a wrapper class (and maybe an interface to sit on top of it) that encapsulates instances of both and synchronises data between them.

    Of course you might get into trouble trying to use instances of that class with other classes that expect one or the other explicitly (rather than through their implemented interfaces, which you could have your class implement as well).

  • 2. Re: Serious Question pertaining to interfaces.
    Zachary1234 Newbie
    Currently Being Moderated

    Yes, I know that one may directly use composition and have two fields, one each for BufferedImage and Rectangle2D, using the latter's intersect

    method to check ahead for acceptable moving before updating the particular Rasters involved et al for the screen.

     

    I do know that you can write an interface which contains actionable code, from one internally supplied abstract class.

     

    I want to write that internal class to extend Rectangle2D to go with an outer interface; in theory so that I now have an interface

    type which has the very Rectangle2D actionable code inside, so I have implement off that method and use setData(raster) to have my desired

    mutable object IMPLEMENT off of a previous class (non interface) type.

     

    Help with this end?

  • 3. Re: Serious Question pertaining to interfaces.
    rp0428 Guru
    Currently Being Moderated

    >

    -Why will the following not compile on my java 1.6?

    >

    If you try to compile it the compiler will give you a message that actually TELLS you why it won't compile.

     

    Then if you post the actual compiler output EVERYONE will know why it won't compile.

    >

    interface SubRectangle2D

    {

    abstract class Rectangle2DImplementation extends Rectangle2D 

                                                                        implements SubRectangle2D

    {

    >

    So you want to define an intertace that contains an abstract class that implements the interface you are defining that hasn't been defined yet?

    Does that make sense to you to try to define something that depends on it already being defined?

  • 4. Re: Serious Question pertaining to interfaces.
    Zachary1234 Newbie
    Currently Being Moderated

    You can always turn a class into a functional interface using composition,

    of the abstract class:

       

       

       interface SubRectangle2D

       {  

          abstract class InnerRectangle2D  implements SubRectangle2D

          {

             private static Rectangle2D.Double RECTANGLE = new Rectangle2D.Double();  

            

             private static String fred = new String("data");

             

             public static void add(double x, double y)

             {

                RECTANGLE.add(x,y);

             }

            

             //.....

    }

  • 5. Re: Serious Question pertaining to interfaces.
    rp0428 Guru
    Currently Being Moderated

    None of what you said addresses ANY of the comments that I made.

     

    Reread my entire reply and address what I said.