Java declaration and access introduction, Java declaration access Introduction

Source: Internet
Author: User

Java declaration and access introduction, Java declaration access Introduction

1. Class Declaration

Declaration of the class itself: for the declaration of the class, it mainly includes the access permission Declaration of the class and the use of non-access modifiers. For a common Java class (POJO), the main access permission modifier only has two public and default permissions, and the internal class can have private permissions. Non-access modifiers mainly include abstract, final, and strictfp.

 

(1) Here we will first discuss non-access modifiers. The following are basic rules:

Abstract: declaring a class is abstract.

Final: declares that a class can be inherited.

Strictfp: declares that all methods in a class follow the IEEE754 rule when processing floating point numbers.

From the above rules, we can draw a conclusion that a class cannot be declared both abstract and final. The essence of an abstract class is that the class that inherits it defines a set of Code of Conduct. Any class that inherits it must implement the abstract method defined by the abstract class. The abstract class cannot be instantiated, abstract classes may not contain abstract methods, but classes containing abstract methods must be declared as abstract classes.

The final class indicates a similar class that cannot be inherited. If you do not want the behavior of a class to be changed, or the behavior of this class is the basis of the entire system, just like the number in mathematics, you can declare this class as final.

 

(2) Class access permission modifier:

First, you need to find out the following aspects of class access:

Create an instance of another class in one class; one class inherits from another class; the methods in one class access the methods and variables of another class. The class's access permission is declared as public, indicating that the class can be accessed by any class. Of course, the access permission to access this class in other classes is the default permission, it indicates that the class in the same package as the class can access it. Note the following points that are easy to ignore:

 

Source File 1:

Package cert;

Class test1 {}

 

Source File 2:

Package cert2;

Import cert. test1;

Public class test2 extends test1 {}

 

Here, test2 produces an error because test1 is the default access permission. test2 uses the inherited method to access test1. Because these two classes are not in the same package, test2 cannot see test1.

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.