Object-oriented three features encapsulation, inheritance, polymorphism

Encapsulation overview: It is an object-oriented programming language that simulates the objective world. In the objective world, member variables are hidden inside the object and cannot be directly manipulated and modified by the outside world.

Encapsulation principle: Hide the content that does not need to be provided externally, hide the properties, and provide public methods to access them. Member method private modification, providing getXxx()/setXxx() methods 

Advantages: The operation of member variables is controlled by methods, which improves the security of the code, encapsulates the code with methods, and improves the reusability of the code.

Overview of inheritance: Inheritance refers to building a new class on the basis of an existing class. The new class constructed is called a subclass, and the existing class is called a parent class. Inherited properties and methods.

Benefits: Improve code reusability, improve code maintainability, create a connection between classes, and provide a prerequisite for polymorphism.

Inherited features:

1. Java classes only support single inheritance, and a class can only have one parent class

2. A class can have multiple subclasses

3. Java supports multiple inheritance

4. No circular inheritance

The contents of the parent class that cannot be inherited:

1. Member variables and member methods modified by private

2. Constructors cannot be inherited

3. Subclasses access parent class member variables through the get\set method

Characteristics of member variables and member methods of child and parent classes after inheritance

1. If there is a subclass, use the subclass. If the subclass does not, use the parent class.

Overview of polymorphism: In the end, polymorphism is reflected in the reference variable of the parent class pointing to the object of the child class. The premise of polymorphism is that there must be an inheritance relationship between the child and parent class or the relationship between the class implementing the interface, otherwise the polymorphism cannot be completed. When the parent class refers to the variable to call the method, the overridden method of the child class will be called.

Features of polymorphic members:

Member variables

When a member variable with the same name appears in the child parent class, when the variable is called polymorphically:

Compile time: The reference is whether there is a called member variable in the class to which the reference variable belongs, if not, the compilation fails.

Runtime: The member variable in the class to which the reference variable belongs is called.

A simple note: compile and run references are on the left.

member method

Compilation time: The reference is to the class to which the reference variable belongs. If the method is not called in the class, the compilation fails.

Runtime: Refer to the class to which the object pointed to by the reference variable belongs, and run the member methods in the class to which the object belongs.

Simple note: compile to see the left change, run to the right.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325202520&siteId=291194637