File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting

 
thomas wilson
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic