T
Tobias K.
Maybe this is an FAQ, but nevertheless I don't get the point.
Look at this:
public class CastTest {
class ClassA {}
class ClassB extends ClassA {}
CastTest() {
ClassA a = new ClassA();
ClassB b = new ClassB();
ClassA c = (ClassA) b;
System.out.println(a.getClass());
System.out.println(b.getClass());
System.out.println(c.getClass());
}
public static void main(String[] args) {
new CastTest();
}
}
If you execute the obove, you'll get the following:
class test.CastTest$ClassA
class test.CastTest$ClassB
class test.CastTest$ClassB
Why is the class of c still ClassB and _not_ ClassA? I casted it!
Is there a way to force the cast from ClassB to ClassA or do I have to
do it by hand - say: Do I have to write a method that takes all members
that are both present in A and in B out of B, then creates a new A and
puts them into it? Or is there another way?
Thanks in advance!
Tobias
Look at this:
public class CastTest {
class ClassA {}
class ClassB extends ClassA {}
CastTest() {
ClassA a = new ClassA();
ClassB b = new ClassB();
ClassA c = (ClassA) b;
System.out.println(a.getClass());
System.out.println(b.getClass());
System.out.println(c.getClass());
}
public static void main(String[] args) {
new CastTest();
}
}
If you execute the obove, you'll get the following:
class test.CastTest$ClassA
class test.CastTest$ClassB
class test.CastTest$ClassB
Why is the class of c still ClassB and _not_ ClassA? I casted it!
Is there a way to force the cast from ClassB to ClassA or do I have to
do it by hand - say: Do I have to write a method that takes all members
that are both present in A and in B out of B, then creates a new A and
puts them into it? Or is there another way?
Thanks in advance!
Tobias