aspose file tools*
The moose likes Beginning Java and the fly likes Problem with objects casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with objects casting" Watch "Problem with objects casting" New topic
Author

Problem with objects casting

Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30

How to realize something like this:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Robert Raps wrote:Problem in pseudocode:
Animal - abstract class
Cat extends Animal
Dog extends Animal


Class pseudoCat = Class.forName("Cat");
Object cc=c.newInstance();

1)How can i cast cc to pseudoCat using something like getClass (not explicitly point to )
2) Is List<Cat> takes object of class pseudoCat??


There is no such class as pseudoCat. You've just retrieved that Class object that has the metadata for the Cat class, and placed a reference to that object into a variable named pseudoCat. It's no different than assigning a variable of any other reference type, such as String, Date, or JPanel.
Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Jeff Verdegan wrote:
Robert Raps wrote:Problem in pseudocode:
Animal - abstract class
Cat extends Animal

Dog extends Animal


Class pseudoCat = Class.forName("Cat");
Object cc=c.newInstance();

1)How can i cast cc to pseudoCat using something like getClass (not explicitly point to )
2) Is List<Cat> takes object of class pseudoCat??


There is no such class as pseudoCat. You've just retrieved that Class object that has the metadata for the Cat class, and placed a reference to that object into a variable named pseudoCat. It's no different than assigning a variable of any other reference type, such as String, Date, or JPanel.

I need take from Class.forName("Cat") class Cat in order to create objects of Class Cat. Something like this:
Class pseudoCat = Class.forName("Cat");
Object cc=pseudoCat.newInstance();
// cc must be instace of Cat!!
List<Cat> list = new ArraList<Cat>();
list.add(cc);

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Robert Raps wrote:
Class pseudoCat = Class.forName("Cat");
Object cc=pseudoCat.newInstance();



Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Jeff Verdegan wrote:
Robert Raps wrote:
Class pseudoCat = Class.forName("Cat");
Object cc=pseudoCat.newInstance();




It is clue, i can't to cast object in this way.
Explain, I want something like this:
String c="cat"
String b="dog"
List<Animal> list=new ArrayList<Animal>;
list.add(new Dog());
list.add(new Cat());

for(Animal entry:list){
i=i+1;
if (i==0){
c obj=(c) entry;
}
else
if (i==1){
b obj=(b) entry;
}
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Instead of



you can try



Otherwise, no, you can only declare a variable to have a type known at compile time. What's your use case for this requirement you say you have?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6


if (i==0){
c obj=(c) entry;
}
else
if (i==1){
b obj=(b) entry;
}



You shouldn't do that. If you're putting them into List<Animal>, then you should be treating them all as Animals, and only calling Animal methods on them. You shouldn't need to cast them.

You almost certainly have a design flaw, but it's impossible to say for sure with a toy example like this.
Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Paul Clapham wrote:Instead of



you can try



Otherwise, no, you can only declare a variable to have a type known at compile time. What's your use case for this requirement you say you have?

My task in school -to write own version Serialization . I meet with problems. I write datas to file something in style of XML. Then i write datas and try to create object of Class which i have read. I don't know what type of object it will be. So, i want to create object in such way

Class<?> theClass = Class.forName(theType);
theType obj = theClass.newInstance;

I try to make this another way:
List<T>=new ArrayList<T extends Animal>;
so when i read my data, i want create object founded class (they all are sub of Animal)
But i don't want to use branch for deciding (if string="cat" then Cat b=new Cat(); list.add(b)) and so on.

I want to
String s=curClass;// it may be Cat, Dog, Pony...
And then:
S object=new S();


Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Jeff Verdegan wrote:

if (i==0){
c obj=(c) entry;
}
else
if (i==1){
b obj=(b) entry;
}



You shouldn't do that. If you're putting them into List<Animal>, then you should be treating them all as Animals, and only calling Animal methods on them. You shouldn't need to cast them.

You almost certainly have a design flaw, but it's impossible to say for sure with a toy example like this.

My task in school -to write own version Serialization . I meet with problems. I write datas to file something in style of XML. Then i write datas and try to create object of Class which i have read. I don't know what type of object it will be. So, i want to create object in such way

Class<?> theClass = Class.forName(theType);
theType obj = theClass.newInstance;

I try to make this another way:
List<T>=new ArrayList<T extends Animal>;
so when i read my data, i want create object founded class (they all are sub of Animal)
But i don't want to use branch for deciding (if string="cat" then Cat b=new Cat(); list.add(b)) and so on.

I want to
String s=curClass;// it may be Cat, Dog, Pony...
And then:
S object=new S();
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

There aren't many problems with creating those objects. You've already found the relevant reflection methods. However your examples all involve assigning those objects to variables of an unknown type. This is where you seem to be stuck.

But fortunately, serialization doesn't have any such requirements. So perhaps you can explain why your version of serialization seems to have that requirement? Where did that come from?
Husam Alsulimani
Greenhorn

Joined: Apr 08, 2013
Posts: 3
as I understand you have a super class such as Animals and sub classes cat and dogs
then you have a list of Animals type which can holds cat and dogs and you need to distinguish between them to take an action
based on the object type in the Animal list

if this is what you want you can use the keyword instanceof


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Husam Alsulimani wrote:
if this is what you want you can use the keyword instanceof


We can, but we probably shouldn't.

Also, in your example, there's no need for instanceof or casting. It could just be written as.


As Paul has already pointed out, there should be no reason for a serialization tool to need to know anything about the types it's processing (other than possibly that they implement Serializable or some other interface). If there's code that's looking at specific classes, it's almost certainly a design flaw.
Husam Alsulimani
Greenhorn

Joined: Apr 08, 2013
Posts: 3
well Thanks jeff

I did a quick search and found that strategy pattern is the solution to the case i had in mind

you are right we should not use instanceof

thanks again
Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Paul Clapham wrote:There aren't many problems with creating those objects. You've already found the relevant reflection methods. However your examples all involve assigning those objects to variables of an unknown type. This is where you seem to be stuck.

But fortunately, serialization doesn't have any such requirements. So perhaps you can explain why your version of serialization seems to have that requirement? Where did that come from?

How can i make Array founded type in order to look through him and print results??
Iterator itr = curMap.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) itr.next();
if (entry.getValue().getClass().isArray()){

}
Robert Raps
Ranch Hand

Joined: Mar 13, 2013
Posts: 30
Jeff Verdegan wrote:
Husam Alsulimani wrote:
if this is what you want you can use the keyword instanceof


We can, but we probably shouldn't.

Also, in your example, there's no need for instanceof or casting. It could just be written as.


As Paul has already pointed out, there should be no reason for a serialization tool to need to know anything about the types it's processing (other than possibly that they implement Serializable or some other interface). If there's code that's looking at specific classes, it's almost certainly a design flaw.

How can i make Array founded type in order to look through him and print results??
Iterator itr = curMap.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) itr.next();
if (entry.getValue().getClass().isArray()){

}
 
Don't get me started about those stupid light bulbs.
 
subject: Problem with objects casting