Scjp test preparation-11

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.