Java: Three characteristics of facing objects

If three characteristics are mentioned, they are: inheritance, encapsulation, polymorphism.
If four characteristics are mentioned, they are: inheritance, encapsulation, polymorphism, and abstraction.

inherit

  1. Concept: Subclasses can inherit all members of the parent class (except the construction method) through the extends keyword

  2. Role: code reuse, simplify code

  3. Members that can be inherited from the parent class: except for the construction method,
    but for privately modified members, they must be accessed indirectly (other public and protected modified methods)

  4. There is single inheritance between classes and multiple inheritance;
    all classes implicitly inherit Object

=======================================================================

Package

  1. The concept is to modify the class or the members of the class through the access permission modifier to achieve the purpose of protecting the safety of internal members

  2. Role: protect internal data

  3. Access modifier: control the scope of access
    public: public, the most privileged, members modified by it, in any directory, can access (all classes)
    protected: protected, in the same package class and subclass Can be accessed
    Default not to write: can only be accessed in the same package class
    private: privatized and modified members can only be accessed in the current class

  4. Four steps of packaging: to meet this standard, it is a JavaBean
    https://blog.csdn.net/ExceptionCoder/article/details/107262709

  5. Note: It
    is not only private that is called encapsulation, private is only the maximum encapsulation.
    Single function principle: Minimize functions, don't think about one method to write all functions. Code reuse rate is high

=======================================================================

Polymorphism

  1. Concept: When creating objects, use parent variables to install subclass objects (upward modeling)

  2. Function: shield the differences of sub-categories, improve the scalability of the program

  3. Upward modeling: Use the parent class object to install the subclass.
    Parent type object name = new subclass type(...);
    object name.method();//If the subclass overrides the method, it will execute the corresponding subclass rewrite The method cannot be adjusted to the unique method of the class.

  4. Downward modeling: forced type conversion (parent rotor), the unique method of the subclass can be called after the conversion.
    Syntax: subclass type variable = (subclass type) parent class variable/parent type value;
    in downward modeling, It should be judged first, use instanceof or parent class variable.getClass() == subclass type.class

  5. The priority order of polymorphic calling methods: if you can't find it in this class, look up the parent class, first find the parent class method, then find the parent class method of the parent class parameter, and finally the parent class of the parent class parameter Method
    eg: find show(O)
    this.show(O)—>super.show(O)—>this.show((super)O)—>super.show((super)O)

=======================================================================

abstract

  1. Concept: There is no method body (that is, no braces), and the method is modified with abstract, with a semicolon after ().

  2. Role: From the grammatical level, all subclasses are forced to rewrite this method to ensure the rationality of business logic

Guess you like

Origin blog.csdn.net/ExceptionCoder/article/details/107338317