Java abstract class and interface Chinese law access modifier problem (

Source: Internet
Author: User

1. Abstract methods in abstract classes (which have an abstract decoration before them) cannot be decorated with private, static, synchronized, native access modifiers. The reasons are as follows: The abstract method has no method body, is used to be inherited, so can not be modified with the private, static modified method through the class name to access the method (that is, the method body), abstract method with static modification meaningless Use the Synchronized keyword to add a lock to the method: And if the keyword is decorated with a static method. The lock used is the lock of the class variable. If it is a decorated class method. The this variable is used to lock. An abstract class cannot instantiate an object, however, because the method is not implemented in the abstract class. is implemented in its subclasses. So. Locks should be owned by their subclasses. So. Abstract methods can not be modified with the Synchronized keyword, native, this thing itself and the abstract conflict, they are the method of the declaration, just a bar method implementation handed over to the subclass, the other is handed over to the local operating system. If it happens at the same time, it is tantamount to handing over the implementation to the subclass and handing the implementation to the local operating system, who is going to implement the specific method?


2, interface is a special kind of abstract class, the method in the interface is all abstract method (but its former abstract can be omitted), so abstract methods in abstract class can not use the access modifier here also can not be used. Also, the protected access modifier cannot be used, because the interface allows all classes to be implemented (not inherited), not just its subclasses, but is modified with public. Interfaces can inherit an existing interface.

Java abstract class and interface Chinese law access modifier problem (

Related Article

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.