The naming and accessing of package structure in parsing Java programming _java

Source: Internet
Author: User
Tags extend modifier

Name of package
the name of the package should avoid conflict with other packages, so choosing a name that is both meaningful and unique is an important aspect of package design. But programmers around the world are developing packages and there is no way to know who is using the package name, so choosing a unique package name is a challenge. If we are certain that a package is used only within our organization, then we can have internal arbitrators (internal arbiter) to ensure that there is no name conflict between the items.

But for the whole world, this approach is impractical. The identifier of the package is a simple name, and a better way to ensure the package name is to use the Internet domain name. If the name of the company we are working for is MAGIC.LNC and the company's domain name is magi c.com, then the declaration of the property package should be:

Package com.magic.attr; Note that the domain name element here is arranged in reverse order of the regular domain name.

If we use this idiom, the package name we use will not conflict with anyone else's package name, except that there may be conflicts within our organization. If there is a real conflict within our Organization (perhaps a large enterprise), then we can use more specific domain names to further qualify. Many large companies have internal subdomains, such as east and Europe, that can be used to further qualify package names:

Package corn. magic.japan.attr;

Using this scenario may make the name of the package very long, but relatively secure. Programmers who use this technique will not choose the same package name, and programmers who do not use this technique will not choose the name we use.

Access to Packages
when declaring the accessibility of the top-level class and top-level interface in a package, there are two options: Package access (package) and public access rights. Classes or interfaces decorated with public can be accessed by code outside the package, types that are not decorated with public have package scopes: they can be accessed by other code in the same package, but they are hidden for code outside the package, and even for code in the child package. When declaring types, we should only declare those types that other programmers need to use as public, and hide those types that belong to the implementation details of the package. This technology gives us a great deal of flexibility because programmers do not rely on the types of implementation details they cannot access, so when we want to change the implementation details, we are free to change them.

Class members that are not declared as public,protected or private can be accessed directly by any code within the package, but the code outside the package is hidden. In other words, the default access modifier is "package", with the exception of the members of the interface whose default access modifier is "public."

A field or method that is not declared private in the package can be accessed by all other code in the package, so the classes in the same package are considered "friendly" or "trustworthy." This allows us to define an application framework that combines predefined codes (predefined code) and placeholder codes (placeholder code), where the placeholder code is overwritten by subclasses of the Framework class. Predefined code can use package access modifiers so that other collaborative code within the package can access them directly, but for outside users, the code is inaccessible. However, the packages in which the code resides are not trusted and vice versa. For example, code decorated with the package access modifier in the package dit cannot be accessed by code in its child package Dit.dat, and vice versa.

Therefore, each type defines three different contracts:

    1. . Publi. Contract: Defines the primary function of a type.
    2. . Protected contract: Defines the functionality that subclasses can obtain for a specific purpose.
    3. Package contract: Defines the functionality that other code within the package can obtain to implement collaboration between types within a package. All these contracts require careful consideration and design.

Accessibility and Cover methods

Only methods that can be accessed in a superclass can be overridden in subclasses. If a method in a superclass cannot be accessed, the method cannot be overridden in a subclass even if the method in the subclass has the same name as the method. When a method is invoked at run time, the system considers its accessibility and determines which concrete implementation to run it.

The following specially constructed example is explained more clearly. Suppose we declare a abstract-base class in the P1 package:

Package P1;

{AB AB AbAb public

abstract class abstractbase

private void pri () {print ("Stractbase.pri ()"):} void Pac () {p  Rint ("Stractbase.pac ()");}

protected void Pro () {print ("Stractbase.pro ()");}

public void Pub () {print ("stractbase.pub ()");}

Public final void Show ()

pri ();

PAC ();

Pro ();

Pub ();

}

  }

In this class, we define 4 methods, each with a different access modifier, and the method body simply identifies itself. Method Show calls the 4 methods in turn on the current object, and when applied to different subclass objects, you can tell which implementation of those methods was invoked.

Now we define the class Concretel, which extends the Abstractbase class, but is located in the P2 package:

Package P2;

Import P1.  Abstractbase public

class Concretel extends abstractbase{public

void pri () {print ("Concretel.pri ()");}

public void Pac () {print ("Concretel.pac ()");}

public void Pro () {print ("Concretel.pro ()");}

public void Pub () {print ("concretel.pub ()");}

}

The 4 methods in the superclass are declared in the class, and their implementations are changed, and these implementations report that they belong to the Con-cretel class. At the same time, their access rights are changed to public for other code access. Execute the following code

New Concretel (). Show ():

Produces the following output:

Abstractbase.pri ()

Abstractbase.pac ()

Concretel.pro ()

concretel.pub ()

Because the private method PRI cannot be accessed by a quilt class (or other Class), the Show method always invokes the implementation of the PRI method in the Abstractbase class. A PAC method with package access in the Abstractbase class cannot be accessed by Concretel, so the implementation of the PAC method in the Concretel class cannot overwrite the definition in the Abstractbase class. So the Show method calls the Abstractbase.pac method. The Pro method and pub method are both accessible in the Concretel class and can be overwritten, so the show method calls the implementation of the two methods in the Concretel class.

Pick up our full class Concrete2 to extend the class Concretel, and then we put it and Abstractbase class in the same package P1 ':

Package P1;

Import P2.  Concretel public

class Concrete2 extends concretel{public

void pri () {print ("Concrete2.pri ()");}

public void Pac () {print ("Concrete2.pac ()");}

public void Pro () {print ("Concrete2.pro ()");}

public void Pub () {print ("concrete2.pub ()");}

}

Because methods in Concretel have public access, they can be accessed in Concrete2, and each method in Concrete2 overrides its corresponding method. In addition, because Concrete2 and ab-stractbase are in the same package, the method Abstractbase.pac can also be accessed in Concrete2, and method Concrete2.pac can be overridden. Call the Show method on the Concrete2 object and print the result as follows:

Abstractbase.pri ()

Concrete2.pac ()

Concrete2. Pro ()

concrete2.pub ()

Finally, we define the class Concrete3 to extend the class Concrete2 and put it in the package P3:

Package P3

Import P1.  Concrete2;

public class Concrete3 extends concrete2{public

void pri () {print ("Concrete3.pri ()");}

public void Pac Q{print ("Concrete3.pac ()");}

public void Pro () {print ("Concrete3.pro ()");}

public void Pub () {print ("concrete3.pub ()");}

The Show method is called on the

Concrete3 object, and the results are printed as follows:

Abstractbase.pri ()

Concrete3.pac () Concrete3.pro ()

conc Rete3.pub ()

Here the method Concrete3.pac appears to override the inaccessible Abstractbase.pac method, but in fact, the method Concrete3.pac overrides the method Concrete2.pac, and method Concrete2.pac overrides the method Abstractbas E.pac, so the method Concrete3.pac indirectly covers the method Abstractbase.pac. By declaring the PAC method as having public access in class Concrete2, you can enable it to be accessed and overwritten by any subclass. '

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.