Java Programming Ideas (2)

Source: Internet
Author: User
Tags class definition export class modifiers


Access Rights control

Access Rights control:

    • Java provides access control modifiers to control access rights.
    • Permissions from large to small, in turn, public,protected, package access (no keywords), private.


Package: library Unit

    • The package contains a set of classes that are organized together under a single namespace.
    • When writing a Java source code file, this file is often used as a compilation unit.
    • There can be a public class within each compilation unit, and the class name must be the same as the file name.
    • If the compilation unit has additional classes, it is not visible outside the package, and these classes are primarily used to provide support for the primary public class.


Code organization:

    • A Java runnable program is a set of. class files that can be packaged and compressed into a single Java document file (jar file).
    • The interpreter is responsible for finding, loading, and interpreting these files.
    • A class library is actually a set of class files. Each of these files has a public class and any number of non-public classes.
    • If you want these artifacts to belong to the same group, you can use the keyword package.
    • If you use the package statement, it must be the first code in the file except for the comment.


The Java interpreter runs the process:

    • First find the environment variable classpath, which is used to find the root directory of the. class file.
    • Starting at the root, the interpreter obtains the package name and replaces each stronghold with a backslash.
    • This results in a path name from the classpath root directory.
    • The interpreter looks in these directories for the. class file that is associated with the class name you want to use.


Java does not have conditional compilation:

    • Java does not have conditional compilation functionality for C.
    • C In most cases, use conditional compilation to solve cross-platform problems.
    • However, the problem does not exist in Java, so conditional compilation is unnecessary.


Package access rights:

    • If you do not provide any access modifiers, it means that the permissions for the current class are "Package access permissions"
    • With package access permissions, all classes in the same compilation unit are automatically accessible.
    • When classes are organized into a package, they are given access to the members of their package access rights.


Public permissions:

    • Use the Public keyword, also known as the interface access permission.
    • The members immediately following the Public keyword are available for each class.


Example07. Default Package

Default package:

    • is in the same directory and does not set itself any package name.
    • Java automatically considers such files to be the default packages that belong to the directory.


Private permissions:

    • Use the Private keyword.
    • In addition to the class that contains the member, no other class can access the member.


Protected permissions:

    • Use the Private keyword, also known as inherited access.
    • The base class can assign access to a member to its derived class through the protected keyword.
    • Protected also provides package access, and other classes within the same package can access the protected element.


The access rights of the class:

    • The rhetorical question permission modifier in Java can be used to determine which classes in the library are available to the consumer of the library.
    • If you want a class to be available to a client programmer, you can do so by acting on the entire class definition with the keyword public.


Some provisions of the class access rights:

    • Each compilation unit (file) can have only one public class.
    • The name of the public class must match exactly the file name that contains the compilation unit.
    • It is also possible for a compilation unit to be completely without the public class. In this case, the file can be arbitrarily named.
    • The class cannot be private or protected.
    • Access to a class has only two choices: package access or public.


For a single case:

    • If you do not want anyone else to have access to the class, you can designate all constructors as private, preventing anyone from creating objects of that class.
    • One exception is that you can create inside a static member of the class.


Example08. Singleton

<--Note:

    • If you do not specify an access modifier for the class access permission, it will default to the package access permission.
    • This means that the object of the class can be created by any other class within the package, but it does not work outside the package.


Reusable classes

ToString Method:

    • Each non-basic type of object has a special method, ToString ().
    • The ToString method of the class is called when the compiler requires a string and an object that currently has only one class.
    • Whenever you want to make a class that you create behave like a string, you only need to write the ToString () method.


Inheritance syntax:

    • With the keyword extends, derived classes automatically get all the fields and methods in the base class.
    • Even if a program contains multiple classes with the Main method, only the main () method of the class called by the command line is called.
    • To inherit, the general rule is to designate all data members as private and all methods as public.
    • Java uses the Super keyword to represent a superclass. The current class is a superclass inheritance.


Base class Initialization:

    • Java automatically inserts a call to the base class constructor in the constructor of the exported class.
    • If you do not have a default base class constructor, or call a base class constructor with parameters, you must use the keyword Super display to write the statement that calls the base class constructor.


Java does not name masking:

    • If the base class has a method name that has been overloaded multiple times.
    • Redefining the method name in the export class does not mask any versions of it in the base class.
    • As a result, the overloaded mechanism works fine regardless of whether the method is defined in the layer or its base class.


Upward transformation:

    • The transformation from an exported class to a base class is generally referred to as an upward transformation.
    • It is always safe to convert from a more specialized type to a more generic type.
    • The compiler still allows for an upward transformation without specifying a special tag.


Final keyword:

    • Depending on the context, there is a slight difference in the meaning of final, usually referring to "this cannot be changed".
    • When the data is decorated, it represents a compiler constant, which must be assigned a value when such constants are defined.
      • A domain that is both static and final can only occupy a certain amount of storage space that cannot be changed.
      • For basic types, final causes the values to be constant.
      • For an object reference, final makes the reference constant. Once the initialization points to an object, it cannot be directed to another object, but the object itself can be modified.
    • When modifying a parameter, this means that the object pointed to by the parameter reference cannot be changed in the method.
    • When modifying a method, using the final method prevents any inheriting class from modifying its meaning, ensuring that inheritance causes the method behavior to be unchanged and will not be overwritten.
    • When a class is decorated, it indicates that the class cannot be inherited.


<--Note: All private methods in a class are implicitly specified as final. Because the private method cannot be taken, it cannot be overwritten.

Initialization and loading of the class:

    • The code for the class is loaded when it is first used.
    • This usually means that loading occurs when the first object of a class is created.
    • It also sounds loaded when accessing the static domain or static method.


After the class is loaded, the object initializes the process:

    • First, all the base types in the object are set to their default values, and the object reference is set to NULL. Complete by setting the object memory to a binary 0 value
    • The base class constructor is then called. The destructors class itself has a base class, and the second base class constructor is called, and so on.
    • The base class constructor, like the export class constructor, undergoes the same process in the same order.
    • After the base class constructor is complete, the instance variables are initialized in their order.
    • Finally, the rest of the constructor is executed.

Example09. Extends and initialization


Polymorphic

Method call Binding:

    • Associating a method call with the same method body is called a binding.
    • If you bind before the program executes, it is called a pre-binding.
    • Binding at run time based on the type of the object, which becomes late binding, also called dynamic binding or runtime binding.


Dynamic binding:

    • The implementation method is to place some kind of "type information" in the object.
    • In addition to the static method and the final method (including the private method) in Java, all other methods are late-bound.


Example10. Polymorphism

Limitations of the Java polymorphism mechanism:

    • The private method is automatically considered the final method and is masked against the exported class and therefore will not be overwritten.
    • If you access a domain directly, the access will be parsed during static compilation.
    • If a method is static, its behavior is not polymorphic.


Example11. Defect of polymorphism (1)
Example12. Defect of polymorphism (2)

Constructor calls for a complex object are in the following order:

    • The storage space allocated to the object is initialized to a binary 0 value
    • Call the base class constructor, this step will be repeated recursively, first of all to construct this level of the root, and then the next layer of export class, until the bottom of the export class.
    • Initializes a method that invokes a member in declaration order.
    • Invokes the constructor principal of the exported class.


Note: The only methods in the constructor that are safe to call are the final methods in the base class, because they are not overwritten.

Example13. Polymorphism of constructor

Covariant return type:

    • Java SE5 adds a covariant return type.
    • It indicates that an overridden method in an exported class can return some type of export of the return type of a base class method.
    • That is, the covariant return type allows a more specific type to be returned than the return type of the overridden base class method.


Example14. Covariant

Downward transformation:

    • In C + +, we have to perform a special operation to get a safe downward transition.
    • In Java, all transformations are checked.
    • Even if it is just a normal type conversion in parentheses, it will still be checked when it enters the run time.
    • If run is to check that the class is not the expected type, it returns a classcastexception, the class transformation exception.
    • The behavior of checking a type during this run is known as "Runtime type Recognition" (RTTI)

Java Programming Ideas (2)

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.