I have the following abstract class:
public abstract class AbstractGroup {
private String name;
.
.
.
}
I have two empty classes that extend this abstract class:
public class GroupA extends AbstractGroup {
}
public class GroupB extends AbstractGroup {
}
Is there a way to cast the following without getting a ClassCastException:
(group is of type GroupA)
group = (GroupB)group;
I need this object instance to become GroupB.
It’s not possible. You cannot cast classes horizontally but only vertically.
GroupAis not the subtype ofGroupBso the exception always will be raised.