The questions are as follows:
Interface Foo {} Class Alpha implements Foo {} class beta extends Alpha {} public class Delta extends beta {public static void main (string [] ARGs) {Beta X = new beta (); // insert code here} Insert the following code into the position shown above, which causes Java. lang. classcastexception:. alpha A = x; B. foo F = (DELTA) x; C. foo F = (alpha) x; D. beta B = (Beta) (alpha) X;
Java. Lang. classcastexception refers to an exception in type conversion. Generally, when forced type conversion occurs, if the actual instance type of the object is smaller than the converted type, classcastexception will be generated at runtime.
In this question, X is a beta instance, which is determined during compilation. Therefore, X can be freely converted in Foo, Alpha, beta, and other lower-level or lower-level types. But he cannot convert it to the Delta class because it does not have the behavior characteristics of the Delta class.
Here, God expands: the code world is actually quite strict, and level-1 Inheritance forms a pyramid structure. The underlying layer is the most primitive superclass or interface, with only the most basic skills. The top layer absorbs the skills at each layer and develops its own unique skills. When an object is born at a level of the pyramid, it can only survive at this level or lower level of birth. Even if it changes to a disguise, although it can be used for a while, it will inevitably show up because of insufficient skills when it is really working. However, there are also some real super objects, although they are naturally noble, but because they are born at a lower level, they can only do what this level or lower level permits. But when needed, they will also put on red underpants, put on a cloak, complete the high level to complete the thing.
Scjp test preparation-11