Java object-oriented explanation

Source: Internet
Author: User
Tags object serialization

It has been a long time since I started to engage in project development. Recently, I have come up with the idea of writing back the basic knowledge I have learned. First, when I first started to learn and access programming, a person stumbled and fumbled forward. When I was a beginner, I understood a lot of things. Later, I learned more things, and some things became clearer; second, after some practices, you can learn some basic things to better understand them. Third, the basics of some things are very important, it is worth doing it.

Object-oriented

Object Oriented is a new kindProgramDesign method, or a new program design specification (paradigm), the basic idea is to use objects, classes, inheritance, encapsulation, polymorphism and other basic concepts for program design. Construct a software system based on objective things (objects) in the real world, and use human natural thinking methods as much as possible in system construction.

Object

An object is an entity used to describe objective things in a system. It is a basic unit of a system. An object consists of a group of attributes and a group of services that operate on these attributes.

Class instantiation can generate an object. The lifecycle of an object consists of three stages: generation, use, and elimination.

When there is no reference to an object, the object becomes a useless object. The Java Garbage Collector automatically scans the dynamic memory area of the object and collects and releases the unreferenced object as the garbage collection. When the system memory is used up or the system. GC () is called to request garbage collection, the garbage collection thread runs synchronously with the system.

Class

A class is a set of objects with the same attributes and methods. It provides a unified abstract description for all objects belonging to the class. It contains two main parts: attributes and methods. In the object-orientedProgramming LanguageA class is an independent program unit. It should have a class name and contain two main parts: attributes and methods.

The class implementation in Java includes two parts: class declaration and class body.

Class Declaration

 
[Public] [Abstract | Final] class classname [extends superclassname] [implements interfacenamelist] {……}

The modifiers public, abstract, and final indicate the class attributes. classname indicates the class name, superclassname indicates the class parent class name, And interfacenamelist indicates the list of interfaces implemented by the class.

Class body

 
Class classname {[Public | protected | private] [static] [Final] [transient] [volatile] type variablename; // member variable [Public | protected | private] [static] [final | Abstract] [Native] [synchronized] returntype methodname ([paramlist]) [Throws exceptionlist] {statements} // member method}

Meanings of member variable qualifiers:

    • Static: static variable (class variable)
    • Final: constant; transient: temporary variable, used for object archiving, used for Object serialization
    • Volatile: Contribution variable for concurrent thread sharing

The implementation of the method includes two parts: Method description and method body.

Method Declaration

Meanings of qualified words in method declaration:

    • Static: class method, which can be called directly by Class Name
    • Abstract: abstract method, no method body
    • Final: The method cannot be overwritten.
    • Native: integrated with other languagesCode
    • Synchronized: controls the access of multiple concurrent threads

The method declaration includes the method name, return type, and external parameters. The parameter type can be either a simple data type or a composite data type (also called a reference data type ).
For simple data types, Java implements value passing. Methods receive parameter values, but cannot change the values of these parameters. If you want to change the parameter value, you can use the reference data type because the referenced data type is passed to the address of the data in the memory. operations on the data in the method can change the value of the data.

Method body

The method body is the implementation of the method. It includes the declaration of local variables and all valid Java commands. The scope of the local variables declared in the method body is within the method. If the local variables have the same name as the class member variables, the class member variables are hidden.
To distinguish between parameters and class member variables, we must useThis. This is used to reference the current object in a method. Its value is the object that calls this method. The return value must be the same as the return type, completely the same, or its subclass. When the return type is an interface, the return value must be implemented.

Constructor

    • Constructor is a special method. Every class in Java has a constructor to initialize an object of this class.
    • The constructor has the same name as the class name and does not return any data type.
    • Overloading is often used for constructor.
    • The constructor can only be called by the new operator.

Basic Features of object-oriented

Encapsulation

Encapsulation is to hide the internal details of an object as much as possible, form a boundary for external entities, and only retain limited interfaces and methods for external interactions.The principle of encapsulation is to prevent external access to and operation on the internal properties of objects, thus avoiding external damage to the internal properties of objects.

You can set certain access permissions for Class Members to hide the information of class members.

    • PRIVATE:Only private members can be accessed by the class itself. If the constructor of a class is declared as private, other classes cannot generate an instance of the class.
    • Default:The members without any access permission restriction in the class belong to the default access status and can be accessed by the class itself and the class in the same package.
    • Protected:A member of a class limited to protected can be accessed by the class itself, its subclass (including the subclasses in the same package and in different packages), and all other classes in the same package.
    • Public:Only public members can be accessed by all classes.

Inheritance

The subclass object has all attributes and methods of the parent class, which is called the inheritance of the parent class by the subclass.

    • In Java, the parent class can have multiple subclasses, but the child class can inherit only one parent class, which is called single inheritance.
    • Inheritance achieves code reuse.
    • All classes in Java are obtained by directly or indirectly inheriting the java. Lang. Object Class.
    • Subclass cannot inherit the member variables and methods whose access permission is private in the parent class.
    • Subclass can override the parent class method, that is, name a member variable with the same name as the parent class.

JavaSuperSuper is used to reference the parent class of the current object. There are three scenarios for super:

    • Access hidden member variables of the parent class, such:Super. variable;
    • Call the method to be overwritten in the parent class, for example: Super. Method ([paramlist]), super () call the parent class constructor;
    • Call the constructor of the parent class, for example, super ([paramlist]);

Polymorphism

Object polymorphism refers to the inheritance of attributes or methods defined in the parent class, which can have different data types or show different behaviors. This makes the same attribute or method have different semantics in the parent class and its sub-classes. For example, the "plotting" method of "ry", "elliptic" and "polygon" are both subclasses of" ry". The "plotting" method has different functions.

Java polymorphism is embodied in two aspects: static polymorphism implemented by method overloading (polymorphism at compile time) and dynamic polymorphism implemented by method rewriting (Runtime polymorphism ).

    • Polymorphism during compilation:During the compilation phase, the compiler determines which method is called to be overloaded.
    • Runtime polymorphism:Because the subclass inherits all attributes of the parent class (except the private class), The subclass object can be used as the parent class object. Any place in the program that uses the parent class object can be replaced by a subclass object. An object can call the subclass method by referencing the subclass instance.

Overloading)

    • Method Overloading is a means for classes to process different data types in a unified manner.
    • Multiple methods can be created in a class. They have the same name, but have different parameters and different definitions.When calling a method, you can determine the method to use by the number and type of different parameters passed to them.
    • The return value types can be the same or different, and the return type cannot be used as the criteria for distinguishing the overloaded functions.

Overriding)

    • Subclass re-writes the method of the parent class. If the method in the subclass has the same method name, return type, and parameter table as its parent class, we say this method is overwritten ).
    • If you need the original method of the parent class, you can use the super keyword, which references the parent class of the current class.
    • The access modification permission of subclass functions cannot be lower than that of the parent class.

 

Related Article

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.