Java Getting Started Notes 4_ access control and packages

Source: Internet
Author: User
1. Access Control in Java
Table 1-1
Visibility/Accessibility
Public
Protected
Package
Private
In the same class
Yes
Yes
Yes
Yes
In the same package
Yes
Yes
Yes
No
In different packages
Yes
No
No
No
In subclasses of the same package
Yes
Yes
Yes
No
In subclasses of different packages
Yes
Yes
No
No
Description
(1) In Java, there is a public, protected, private three display modifiers to control visibility, package is not a display modifier, it is implied, that is, if the class, variable, etc. before the display of the visibility modifier, it is package level. If package is not specified in the definition of a class, then Java puts it in the default package, which is typically the current directory.
 
(2) methods in subclasses if the method of the parent class is overloaded, the visible level of the method should be more or the same, such as if the method in the parent class is public, then the method in the subclass must be public.
(3) In Java, in general, variable members are preferably private, and access to them can be done by public methods, in which some control can be made to ensure the consistency of the data. These method names are generally prefixed with get and set.
2. Package
The package name is generally lowercase, and the first letter of the class name is generally uppercase, so that when referenced, the package name and class name can be clearly distinguished. If the package name is not defined using package before the definition of the class, then the class is the default package.
2.1 Hello Package
First, learn the use of package by a simple package definition:
Package testpackage;
public class Test
{
public static void Main (String args[])
{
Mnode node = new Mnode ();
Node.print ("Hello");
}
}
Description
(1) using the Package keyword to indicate the package in which the class resides;
(2) The package statement must be at the front of the file;
(3) Compile-time can use Javac

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.