wood burning stoves*
The moose likes Beginning Java and the fly likes casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting" Watch "casting" New topic
Author

casting

thomas wilson
Greenhorn

Joined: Sep 21, 2004
Posts: 24
In the following codes, could someone tell me why I got error when trying to cast Object back to Orange or Apple? How can one cast the object back to its original type? Thank you

========================

import java.util.*;
import java.lang.reflect.*;


public class test1 {


public static void main(String args[]) {
List fruits = new ArrayList();
Orange orange = new Orange();
orange.setIt("ORANGE");
Apple apple = new Apple();
apple.setIt("APPLE");

fruits.add(orange);
fruits.add(apple);

ListIterator itefruits = fruits.listIterator();

while (itefruits.hasNext()) {
Object obj = itefruits.next();
Class c = obj.getClass();
String classname = obj.getClass().getName();
//this statement below gave me error message "cannot find symbol class c"
System.out.println("orange or apple? : "+((c)obj).getIt());

}


}

}

class Orange {
private String o;

public void setIt(String o1) {
o = o1;
}

public String getIt() {
return o;
}
}

class Apple {
private String a;

public void setIt(String a1) {
a = a1;
}

public String getIt() {
return a;
}
}
Krishnan Loganathan
Greenhorn

Joined: Apr 24, 2004
Posts: 23
Hi,
you can cast an object to the ClassName only. Here u are giving a Class object that contains the classname. And JVM searches whether there is a class 'c' exists. If it cannot able to find 'c', it will throw error. you can reflection concept to solve your problem. Otherwise u have to give proper classname as either Orange/Apple. To solve this, you can declare a interface and that extended by Orange/Apple. So whenever u are getting the Object, just cast it to the Interface, and call that method (Runtime Polymorphism). Here i had attached the code, that will use reflection concept.


import java.util.*;
import java.lang.reflect.*;


public class test1 {


public static void main(String args[]) throws Exception {
List fruits = new ArrayList();
Orange orange = new Orange();
orange.setIt("ORANGE");
Apple apple = new Apple();
apple.setIt("APPLE");

fruits.add(orange);
fruits.add(apple);

ListIterator itefruits = fruits.listIterator();

while (itefruits.hasNext()) {
Object obj = itefruits.next();
Class c = obj.getClass();
String classname = c.getName();
Method method = c.getMethod("getIt", null);

//this statement below gave me error message "cannot find symbol class c"
//System.out.println("orange or apple? : " + ((c)obj).getIt());
System.out.println("orange or apple? : " + method.invoke(obj,null));

}


}

}

class Orange {
private String o;

public void setIt(String o1) {
o = o1;
}

public String getIt() {
return o;
}
}

class Apple {
private String a;

public void setIt(String a1) {
a = a1;
}

public String getIt() {
return a;
}
}

Regards,

Loga
 
Consider Paul's rocket mass heater.
 
subject: casting