Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-09-2007, 02:23 PM
Member
 
Join Date: Jul 2007
Posts: 5
leebee is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-09-2007, 04:53 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
If you're talking about

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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-10-2007, 12:57 PM
Member
 
Join Date: Jul 2007
Posts: 5
leebee is on a distinguished road
cheers mate, so how would i cast ImageWindow to Window??
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-10-2007, 01:09 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
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:

Code:
Window w = new ImageWindow();
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-10-2007, 02:11 PM
Member
 
Join Date: Jul 2007
Posts: 5
leebee is on a distinguished road
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???????
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-10-2007, 02:24 PM
Member
 
Join Date: Aug 2007
Posts: 9
rhobincu is on a distinguished road
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:

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:

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting an int value into a char kurtulas New To Java 2 02-16-2008 10:03 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 02:06 PM
'Casting' couch !!!! ajaygargnsit Advanced Java 4 01-04-2008 06:54 PM
'Casting' couch !! ajaygargnsit New To Java 1 12-22-2007 03:05 PM


All times are GMT +3. The time now is 10:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org