Java object-oriented-package and access controller, java object-oriented

Source: Internet
Author: User

Java object-oriented-package and access controller, java object-oriented

 

When multiple people develop the same project, the same class name may occur. Package is a measure to avoid repeated class or interface names. In fact, a package is a folder with a certain level of structure. * The class file must be saved in the folder corresponding to the package declared by the current class.

Package Definition Format: package name [. Sub-package name];

During Compilation: The javac-d. Hello. java-d option generates the corresponding directory structure for the source file.

If no package name is set for the source file, java automatically regards such a file as the default package that belongs to the directory.

The parent package and the Child package only indicate a logical connection and there is no relation in usage. If the class in the parent package needs to use the class in the Child package, the full name of the child package must be used, the parent package cannot be omitted.

If several classes are stored in different packages, you need to use the full name of the class when calling the constructor. You can use the import keyword to import classes at the specified package level to a java file, so that the package prefix can be omitted. Classes in the same package can be accessed directly without import.

The * number in the import Statement can only represent a class, not a package.

By default, java imports all classes under the java. lang package to all source files. If a class with the same name exists, you must use the full name of the class.

If all the methods in a class are static methods declared in static mode, you can use the "import static" method to import them during the import process. The "class" is not used during the call. method (), directly call the static method or access the static property.

Package com. servlet; // All package names are in lower case import java. io. fileReader; // import a specific class: import package name + class name import static java. lang. math. *; // import static attributes import java. util. *; // java. util. sub. * classes under the sub-package are not imported

Encapsulation is one of the three main characteristics of object orientation. It refers to hiding the object state information and implementation details inside the object and does not allow external programs to directly access the internal information of the object, instead, the internal information is operated and accessed through the methods provided by this class.

The class has only two access delimiters: public and default.

Each java source code file can have multiple classes, but classes and classes are at the same level and cannot be nested in the class. Only one public class is allowed. The class name is the same as the file name. Other classes in the source code file provide support for the public class and are invisible outside the package.

When modifying a class library, how does a class library developer ensure that the class library user does not need to modify the code?

Java uses three keywords to set the boundaries inside the class: public, private, and protected. These access specifier determine where the things that follow them can be used.

The access qualifier controls where members (attributes or methods) can be accessed (by '.') and where they cannot be accessed. If this rule is not followed, an error occurs during compilation.

Although the Instance Object of the class has all the Members defined in the class, the instance object does not have direct access to its own members anywhere. It depends on whether the class of the Instance Object meets the access limit.

Class Members have four access delimiters:

Default: the package access permission. Any class in the same package can be accessed without any modifier.

Private: It can only be accessed in the current class (the type creator and the internal method of the type. Other classes and subclasses of the same package cannot be accessed. To access the object, you can define the public get method to obtain the private attribute in the method. In addition to this class, the public method can be accessed by. And the private attribute cannot. Reserves the right to change the underlying implementation.

Public: public, visible to all classes (the same package or different packages.

Protected: inherits the access permission and is visible to classes in the same package and all subclasses (same package or different packages.

Protected access requires a little more elaboration. suppose class A declares a protected field x and is extended by a class B, which is defined in a different package (this last point is important ). class B inherits the protected field x, and its code can access that field in the current instance of B or in any other instances of B that the code can refer. this does not mean, however, that Code of class B can start reading the protected fields of arbitrary instances of! If an object is an instance of A but is not an instance of B, its fields are obviously not inherited by B, and the code of class B cannot read them.

[It is not in the same package as the base class. When a derived class is generated, the derived class can inherit the protected member (subInstanc. x (), the inherited protected member is accessible within the derived class (super. x (), but within the derived class, you cannot directly access the protected member of the base class instance object or the sibling class (inherited from the same base class) the instance object inherits the protected field (a derived class can only access the base class protected member inherited by the object of the derived class, you cannot access the protected member of the base class inherited by other derived class objects of the base class ).]

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.