aspose file tools*
The moose likes Beginning Java and the fly likes How to cast one class into another non related class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to cast one class into another non related class" Watch "How to cast one class into another non related class" New topic
Author

How to cast one class into another non related class

Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Hi,


I have two classes one is TableFileItem and other is FtpFile. There is no relation in between them, in a third class FtpBrowserEngine there is a method named deleteRecursive(TableFileItem). This method takes argument of TableFileItem type and checks whether it is a file or a directory.If it is a file then methode deletes it, if it is a directory then this method has to call itself. This should be done in a recursive way. But the problem is that method inside deleteRecursive(TableFileItem) method returns a FtpFile object, and to call deleteRecursive(TableFileItem) i need to cast FTPFile object into TableFileItme. Can anyone tell me how can i cast FTPFile in to TableFileItem.


Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

In java everything is Object. So you can cast to Object type and then again cast it to some different type of Object based on some conditions (like Object.getClass()).

Obviously I sense that you are not making good use of inheritance because in Java everything may it be a File or Directory share the same inheritance group. I will suggest to correct the inheritance hierarchy if you have time otherwise for a quick fix supercasting and then subcasting should solve the problem.


My Blog SCJP 5 SCWCD 5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Himanshu Gupta wrote:In java everything is Object. So you can cast to Object type and then again cast it to some different type of Object based on some conditions (like Object.getClass()).

Which will lead to a ClassCastException if you cast to the wrong class, so that's not going to work here.

Pradeep, you will need to have a mapping from FtpFile to TableFileItem somehow. Either use a Map for that, or store the FtpFile in the TableFileItem and then loop through all items to find the matching one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
If they are unrelated, you can't cast them.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Here is the sample of what I mean


I am sure that this will not give any exceptions and casting can be done inside if block.

In java everything is Object. So you can cast to Object type and then again cast it to some different type of Object based on some conditions (like Object.getClass()).


I know its lame in big projects where inheritance depth can go deeper and it can become a mess later on. I still stress to have a right Inheritance line.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
If you get that sort of problems you may not really have a right inheritance line.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Himanshu Gupta wrote:

How about using the instanceof operator: That is shorter and easier to read.

And your code still cannot convert an FtpFile into a TableFileItem, and that was the initial question.
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Thankx Himanshu ,

Your solution is not working.


As the code shown above you are assigning Person and Addr into Object class. By doing so person and addr lost their original identity and they became object of Object class only.So now there is neither Person nor Addr remained only Object is there.

According to Campbell Ritchie

If they are unrelated, you can't cast them.
.

To sort this problem i made a interface and implemented in both the classes, still i am not able to cast them. Can you tell another sort of problem.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

addr lost their original identity and they became object of Object class only.So now there is neither Person nor Addr remained only Object is there.


How are you sure that addr lost its identity. Ask the Object using instaceOf or getClass() function. They can say a lot of their origin and can come back if they find their original type (Person or Addr) without throwing ClassCastException.

And yes in Java we live in a world of Objects so you can play around Objects.

HTH
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Rob Prime wrote:
Himanshu Gupta wrote:

How about using the instanceof operator: That is shorter and easier to read.

And your code still cannot convert an FtpFile into a TableFileItem, and that was the initial question.



Yeah I agree. instaceOf is better.
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
I want to say that


with obj1 you will not be able to access the members of Person class.

Also please have a look at this too
To sort this problem i made a interface and implemented in both the classes, still i am not able to cast them. Can you tell another sort of problem.

Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Pradeep, you will need to have a mapping from FtpFile to TableFileItem somehow.


Can you tell me how??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

I think I gave some hints right after the sentence you quoted.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

IS there any problem in casting the Object back to its type once you know which type of Object it is so that you can access any of its methods ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Pradeeep Sharma wrote:Thankx Himanshu
It's thanks, not thankx, please, for reasons explained here.

To sort this problem i made a interface and implemented in both the classes, still i am not able to cast them. Can you tell another sort of problem.
Declare your objects as both being of the type of that interface, then there is no need for casting.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Himanshu Gupta wrote:IS there any problem in casting the Object back to its type once you know which type of Object it is so that you can access any of its methods ??

No there isn't, but the problem of this thread is not casting but converting. Casting is only possible for a single object, from one reference type to another. Converting is taking one object and get a matching (usually different) object for it, possibly creating it but not necessarily. One form of conversion could be taking a File object and turning it into a byte[] object by reading its contents. That's never going to happen with casting.
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
I did

as you said
Declare your objects as both being of the type of that interface, then there is no need for casting.



Now to access any method for example table.isFile() ,this method should be declared in interface as well as in both the classes. Now the question is that if i would have all the common method in both the classes, then there is no need for declaring any Interface.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Rob Prime wrote:
Himanshu Gupta wrote:IS there any problem in casting the Object back to its type once you know which type of Object it is so that you can access any of its methods ??

No there isn't, but the problem of this thread is not casting but converting. Casting is only possible for a single object, from one reference type to another. Converting is taking one object and get a matching (usually different) object for it, possibly creating it but not necessarily. One form of conversion could be taking a File object and turning it into a byte[] object by reading its contents. That's never going to happen with casting.


Thanks. I got the point. I think I misunderstood the problem.

But the problem is that method inside deleteRecursive(TableFileItem) method returns a FtpFile object, and to call deleteRecursive(TableFileItem) i need to cast FTPFile object into TableFileItme.


@Pradeep

Now the question is that if i would have all the common method in both the classes, then there is no need for declaring any Interface.

Answers is adherence to standards. It is always beneficial have interfaces. After sometime you may want to make a class which represents some other form of file or directory.


Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Alright i got it, the use of interfaces. But my problem is still as it was. So can any one tell me some rules of casting. So that i can relate my problem with any of them and find a solution.
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
Please give attention at this


can any one tell me some rules of casting
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Pradeeep Sharma wrote:Please give attention at this
Patience, please. It's only 7 hours, most of which time we were asleep.

Java™ Language Specification: look for the two sections about converting reference types.
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
I am really sorry for that .
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Pradeeep Sharma wrote:Alright i got it, the use of interfaces. But my problem is still as it was. So can any one tell me some rules of casting. So that i can relate my problem with any of them and find a solution.


One important rule is that casting of objects never convert the objects. That means you can never cast an object into something it isn't already.

Another important rule is that you should avoid casting objects in the first place. It's okay to upcast to a supertype (because you really don't have to cast in that case). But to downcast to a subtype is bad because it's not typesafe.

The suggestion to introduce a shared interface for the (previously) unrelated classes is a good solution because it removes the need for casting.
Pradeeep Sharma
Ranch Hand

Joined: Dec 04, 2008
Posts: 32
This was the real answer i was waiting for. This is really helpful, Thanks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to cast one class into another non related class