Scjp test preparation-5

Source: Internet
Author: User

The following code inserts the code at the specified position to compile properly:

 
ClassAlpha {Public VoidBar (Int... X ){};Public VoidBar (IntX ){};}Public ClassBetaExtendsAlpha {//Insert code here}

The following options are available:

A:Private   Void Bar ( Int  X) {} B:  Public   Void Bar ( Int  X) {} C:  Public   Int Bar (string X ){ Return 1 ;} D:  Public Alpha bar ( Int  X) {} e: Public   Void Bar ( Int X, Int  Y) {} f:  Public   Int Bar ( Int X ){ Return X ;}

 

The focus of this question is rewriting, and heavy loading is the smoke screen.

The feature of method rewriting can be summarized as "two are the same, two are big and small ".

The two are the same: the method name is the same, and the parameter list is the same;

Two smaller values mean that the return value type of the subclass method is smaller or equal than the return value type of the parent method, the exception classes thrown by the subclass method declaration are smaller or equal than the exception classes thrown by the parent class method declaration;

Generally, the access permission of the subclass method is greater or equal to that of the parent method (Private <default <protected <public ).

There is also a restriction: both the override method and the parent class method are either instance methods or class methods, but not instance methods or class methods. Otherwise, a compilation exception occurs.

 

The feature of heavy load can be summarized as "two different"

The two methods are in the same class and have the same method name;

One difference is that the parameters are different.

In addition, method overloading has nothing to do with the return value type and modifier.

In this question, only the parent class involves overloading. Therefore, you do not need to consider the overload issue during judgment.

 

Determining whether a method can be correctly compiled can be based on a very simple principle: When a method is called, will the computer be confused, that is, whether the instructions sent to the computer are clear. Currently, computers have recorded accurate computation to execute USER commands. If the user's command is not clear enough, the computer will not be able to judge, and the computer will stop executing an exception.

 

The options in the above question are all the same method names. You only need to determine whether the method is overwritten or new:

A: The method name is the same, and the parameter list is the same, such as rewriting, but the access permission is less than the parent class method;

B: Correct rewrite;

C: The method name is the same, but the parameters are different. It is a new method;

D: the return value type is incorrect. The value is overwritten;

E: the parameters are different. It is a new method;

F: indicates the type of the returned value. It indicates an incorrect rewrite;

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.