Java prerequisites: classes and objects

Source: Internet
Author: User

1. Object-oriented features

  • Java supports three features of object-oriented:

    • Encapsulation: Java provides three access control modifiers: private, protected, and public;

    • Inheritance: provides the extends keyword for the subclass to inherit the parent class. The subclass to inherit the parent class can inherit the Field and method of the parent class. If access control permits, subclass instances can directly call the methods defined in the parent class, which is a means of reuse;

    • Polymorphism: When inheritance is implemented, the subclass object can be directly copied to the parent variable, which has polymorphism;

      • In addition to this inheritance, you can also use combinations to achieve reuse;

        Ii. Category

        • Reference variable: When a class is a type of custom data, you can use a class to define variables. These variables are collectively referred to as reference variables;

          • Java programming has two important concepts:

            • Class: the abstraction of a batch of objects. classes can be understood as some concept;

            • Object: an Object is a specific entity;

            • Definition Syntax:

              [Modifier] class name {

              Zero to multiple constructor definitions...

              Zero to multiple fields...

              Zero to multiple methods...

              }

              • Modifier: It can be pubilc, final, abstract, or completely omitted;

              • Class Name: a valid identifier;

              • Field: defines the status data contained in the instance of this class or this class;

              • Method: Define the behavior characteristics of instances of the class or class;

              • Constructor: used to construct instances of this class;

                • Define Field Syntax:

                  [Modifier] Field name of Field type [= default value];

                  • Modifier: either public, protected, private, static, or final can be omitted. public, protected, or private can only appear at most, it can be combined with static and final to modify the Field;

                  • Field Type: It can be any type of Java design language, including basic type and reference type;

                  • Field name: a valid identifier;

                  • Default Value: specify a default value;

                    • The syntax format of the definition method is as follows:

                      [Modifier] method return value type method name (parameter list ){

                      // Method body composed of zero to multiple executable statements

                      }

                      • Modifier: the modifier can be omitted. It can be public, private, static, final, and abstract. Among them, public, protected, and private can only appear at most; abstract and final can only appear at most one of them; they can be combined with static for modification;

                      • Return Value: any type supported by Java;

                      • Method Name: The name is the same as that of Field;

                      • Parameter List: defines the acceptable parameters for this method. Multiple parameters are separated by commas;

                        • The constructor is a special method. The definition syntax is similar to the method. The syntax format is as follows:

                          [Modifier] constructor name (parameter list ){

                          // The constructor execution body composed of zero to multiple executable statements

                          }

                          • Modifier: It can be omitted, which can be one of public, protected, and private;

                          • Constructor name: it must be the same as the class name;

                          • Parameter list: the format of the parameter is the same as that of the method;

                            Iii. generation and use of objects

                            • The fundamental way to create an object is the constructor. You can use the new keyword to call the constructor of a class to create an instance of this class;

                              • Class or instance access method or Field Syntax: class. Field | method, or instance. Field | method;

                              • Static modification methods and fields can be called through classes or through instances. common methods and fields without static modification can only be called through instances;

                                4. Objects, references, and pointers

                                • The variable of the Person type defined by the program is actually a reference, which is put in the stack memory and points to the actual Person object; the real Person object is placed in the heap memory;

                                • In fact, the reference in Java is the pointer in C, but the Java language encapsulates the pointer to avoid tedious pointer operations;

                                • Objects in heap memory can have multiple references, that is, multiple reference variables point to the same object;

                                  • If the object in the heap memory does not have any variables pointing to this object, the program will not be able to access this object, and this object will become garbage, and the Java garbage collection mechanism will recycle this object, releases the memory occupied by this object;

                                  • If you want to notify the garbage collection mechanism to recycle an object, you only need to cut off all the reference variables of the object and their associations, that is, assign these Application variables to null;

                                    5. this reference of an object

                                    • Java provides a this keyword, which always points to the object that calls this method. Depending on the location of the this keyword, this is the default application of the object in two scenarios:

                                      • The constructor references the object that the constructor is initializing;

                                      • Apply the object that calls the method in the method;

                                      • This can represent any object. When this occurs in a method body, it indicates that the object is uncertain, but its type is determined. It indicates that the object can only be the current class; only when this method is called can the object that it represents be determined. this indicates who is calling this method;

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.