Introduction to the Declaration and access of Java

Source: Internet
Author: User
Tags modifiers

1. Declaration of class

Declaration of the class itself: for the declaration of a class, it mainly includes the access rights declaration of the class and the use of the non-access modifier. For a common Java class (POJO), the primary access modifier has only two public and default permissions, and the inner class can have private permissions. The non-access modifiers mainly include abstract,final and STRICTFP.

(1) The non-access modifiers are discussed here, and the following are the 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 working with floating-point numbers.

One conclusion from the above rules is that a class cannot be declared both abstract and final. The essence of an abstract class is to define a set of code of conduct for the class that inherits it, and any class that inherits it must implement an abstract method defined by an abstract class that cannot be instantiated, an abstract class may not contain an abstract method, but a class containing an abstract method must be declared as an abstract class.

The final class represents a similar one that cannot be inherited. If you do not want the behavior of a class to be changed, or if the behavior of the class is the basis of the whole system, as in mathematical numbers, you can declare the class as final.

(2) The access modifier for the class:

The first thing to figure out is what the class's access includes:

Creates an instance of another class in one class; A class inherits from another class, and a method in one class accesses methods and variables of another class. The access rights of a class are declared public, which means that the class can be accessed by any class, and of course, access to that class in other classes is the default permission, which means that the class under the same package can access it, where the following are relatively easy to overlook:

Source file One:

Package cert;

Class Test1 {}

Source file Two:

Package cert2;

Import Cert.test1;

public class Test2 extends test1{}

Here the Test2 class generates an error because test1 is the default access, Test2 accesses test1 using inheritance, and because these two classes are not in the same package, Test2 cannot see test1.

Introduction to the Declaration and access of Java

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.