basic casting should be
MyClass mc = (MyClass)aClass
that is easy
but based on my program, I don’t know the class name until runtime.
for example, the class name could be interp_0, interp_1, interp_2, interp_3 …….#;
Is there anyway in java that I could use to cast it?
For now All I got is
Class afterCast = Class.forName(“Interp_” + countState);
but what I want is
(“Interp_” + countState) afterCast
, not
Class afterCast
.
Thanks for all of you who help me. It is so quick than I expected.
I would suggest using an interface implemented by all these classes which contains the methods you need. You can then cast to this interface. I don’t see why this should be done different because you know which methods you expect at those objects which is an interface actually.