Results 1 to 6 of 6
Thread: Casting
- 08-09-2007, 12:23 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 5
- Rep Power
- 0
- 08-09-2007, 02:53 PM #2
Member
- Join Date
- Aug 2007
- Posts
- 9
- Rep Power
- 0
If you're talking about
then you can't.Java Code:ij.gui.ImageWindow extends java.awt.Frame
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
- 08-10-2007, 10:57 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 5
- Rep Power
- 0
cheers mate, so how would i cast ImageWindow to Window??
- 08-10-2007, 11:09 AM #4
Member
- Join Date
- Aug 2007
- Posts
- 9
- Rep Power
- 0
"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:ImageWindow iw = new ImageWindow(); //... Window w = (Window)iw;
Java Code:Window w = new ImageWindow();
- 08-10-2007, 12:11 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 5
- Rep Power
- 0
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???????
- 08-10-2007, 12:24 PM #6
Member
- Join Date
- Aug 2007
- Posts
- 9
- Rep Power
- 0
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:
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.Java Code:ImageWindow iw = new ImageWindow(); Window w = (Window)iw; w.show(fname, x, y);
You can do like this:
Now you're casting the object back to ImageWindow so the proper method is invoked.Java Code:((ImageWindow)w).show(fname, x, y);
Maybe it'd be easy to let us know what you're trying to do, it seems awfully hard. :)
Similar Threads
-
Casting an int value into a char
By kurtulas in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:03 PM -
Type Casting Help
By rhm54 in forum New To JavaReplies: 2Last Post: 02-07-2008, 12:06 PM -
'Casting' couch !!!!
By ajaygargnsit in forum Advanced JavaReplies: 4Last Post: 01-04-2008, 04:54 PM -
'Casting' couch !!
By ajaygargnsit in forum New To JavaReplies: 1Last Post: 12-22-2007, 01:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks