Results 1 to 6 of 6

Thread: Casting

  1. #1
    leebee is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default Casting

    I am, trying to cast window(from java.awt) to be of type ImageWindow(from utilities package)

    I have tried

    window = (ImageWindow) window

    it seems to work but i aint 100% sure

    Lee

  2. #2
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    If you're talking about

    Java Code:
    ij.gui.ImageWindow extends java.awt.Frame
    then you can't.

    java.awt.Window is higher on the class hierarchy (towards the root) than ImageWindow.

    You can cast ImageWindow to Window but not the other way around.

    To simply put it, once you create an object of type ImageWindow, this object contains all information (fields and method access) of an object of type Window and more. So you CAN cast ImageWindow to Window.

    If you create an object of type Window, this object will not have any information of the extended classes (java.awt.Frame and ij.gui.ImageWindow). So you CAN'T cast Window to ImageWindow.

    Enough babbling from me,
    Good luck

  3. #3
    leebee is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default

    cheers mate, so how would i cast ImageWindow to Window??

  4. #4
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    ImageWindow iw  = new ImageWindow();
    //...
    Window w = (Window)iw;
    "iw" is a new object of Type ImageWindow. If you want to store the reference of "iw" in a local variable of type Window, you declare the variable and then cast the object "iw" to type Window as above. You can also do it like this, it does the same thing without using the "iw" object reference:

    Java Code:
    Window w = new ImageWindow();

  5. #5
    leebee is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default

    cheers mate that works or at least the compiler get past the lines of code, but it stops at the next line which used to be iw.show(string, int, int) well actually show(fname, x, y)

    But i have just cast it so in theory it should also work as w.show(fname, x, y) but the compiler says its an error, saying Window doesnt have a method show() where ImageWindow does.

    Why wont it compile???????

  6. #6
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    Well, I know for sure java.awt.Window doesn't have a method show(String, int, int). If the class ImageWindow has it, then make sure the compiler knows what class your object belongs to.

    If you do:

    Java Code:
    ImageWindow iw = new ImageWindow();
    Window w = (Window)iw;
    w.show(fname, x, y);
    it is most probable it won't work because you're telling the compiler to call (invoke) the method show(String, int, int) in class Window (since you cast iw to Window) and this class doesn't have such a method.

    You can do like this:

    Java Code:
    ((ImageWindow)w).show(fname, x, y);
    Now you're casting the object back to ImageWindow so the proper method is invoked.

    Maybe it'd be easy to let us know what you're trying to do, it seems awfully hard. :)

Similar Threads

  1. Casting an int value into a char
    By kurtulas in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 09:03 PM
  2. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 01:06 PM
  3. 'Casting' couch !!!!
    By ajaygargnsit in forum Advanced Java
    Replies: 4
    Last Post: 01-04-2008, 05:54 PM
  4. 'Casting' couch !!
    By ajaygargnsit in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •