Explain in detail the actual operation of encapsulation, inheritance and polymorphism


Object-oriented three major features: encapsulation, inheritance, polymorphism


**

encapsulation

**

The embodiment of encapsulation in java:
1. The method is a kind of encapsulation.
2. The keyword private (privatization) is also a kind of encapsulation.
Encapsulation is to hide some detailed information, which is invisible to the outside world
. The class can still be accessed at will, but it cannot be accessed directly outside the scope of this class

**

inherit

**
Inheritance is the premise of polymorphism. If there is no inheritance, there will be no polymorphism. The
main problem solved by inheritance is: commonality extraction.
The characteristics of the inheritance relationship:
1. Subclasses can have the "content" of the parent class
2. Subclasses can also have own proprietary content

In the inheritance relationship, "a subclass is a parent class", that is to say, a subclass can be treated as a parent class.
In the parent-child inheritance relationship, if the member variables have the same name, when creating a subclass object, access has two ways

Direct access to member variables through subclass objects: whoever is on the left of the equal sign will take priority, and if there is no member variable, access
member variables indirectly through member methods: whoever the method belongs to, use whoever is preferred, and if there is no one, look up

Local variables: directly written as variable names
Member variables of this class: this.Member variable name
Parent class member variables: super.Member variable name

In the inheritance relationship between parent and child classes, the rules for creating subclass objects and accessing member methods:
who is the created object, who is the first to find, if not, then look up

Note:
Whether it is a member method or a member variable, if there is no member method, it will look up the parent class, and it will never look down for the subclass

Override
concept: in the inheritance relationship, the name of the method is the same, and the parameter list is the same

Override (Override): The name of the method is the same, and the parameter list [is the same]. Override, override
Overload (OverLoad): the name of the method is the same, the parameter list [different]

Precautions for method overriding and rewriting:
1. It is necessary to ensure that the names of the methods between the parent and child classes are the same. The parameter list
@Override is written in front of the method to check whether the overriding and rewriting is valid and correct. This annotation is not written, as long as it meets The requirement is also the correct method to override and override
2, the return value of the subclass method must be [less than or equal to] the scope of the parent method.
Small extension hint: The java.lang.Object class is the highest public parent class (ancestor class) of all classes, and java.lang.String is the subclass
3 of Object. The permissions of the subclass methods must be [greater than or equal to] the permissions of the parent class methods modifier.
Small extension tips:: Public > Protected > (default) > private.
Remarks: (default) is not the keyword default, but write nothing, leave blank.

In the inheritance relationship, the access characteristics of the parent-child class construction method:
1. There is a default implicit "super()" call in the sub-class construction method, so it must be the parent class construction that is called first, and the sub-class construction that is executed second
. The subclass structure can use the super keyword to call the parent class overload structure
3. Super's parent class construction call must be the first statement of the subclass construction method, and a subclass cannot call the super structure multiple times.
Summary:
The subclass must call the parent class construction method. If it is not written, it will be presented with super(). If it is written, it will be called with the specified super. There can only be one super, and it must be the first one.

There are three usages of the Super keyword:
1. In the member method of the subclass, access the member variable of the parent class.
2. In the member method of the subclass, access the member method of the parent class.
3. In the construction method of the subclass, access the construction method of the parent class.

The super keyword is used to access the content of the parent class, and the this keyword is used to access the content of this class. There are three usages:
1. In the member methods of this class, access the member variables of this class
2. In the member methods of this class , to access another member method of this class.
3. In the construction method of this class, access another construction method of this class.
Note in the third usage:
A.this (…) call must also be the first statement of the construction method, the only two
construction calls B.Super and this cannot be used at the same time.

Three characteristics of Java inheritance:
1. The Java language is single inheritance. The direct parent class of a class can only be unique. 2. The
Java language can be multi-level inheritance
. 3. The direct parent class of a subclass is unique, but one A parent class can have many subclasses.

Abstract method: If the method in the parent class is not sure how to implement the {} method body, then this should be an abstract method Abstract method:
add the abstract keyword, then remove the curly braces, and end with a semicolon, the abstract method The content is uncertain
Abstract class: the class where the abstract method is located must be an abstract class, just write abstract before the class

How to use abstract classes and abstract methods:
1. New abstract class objects cannot be created directly.
2. A subclass must be used to inherit the abstract parent class.
3. Subclasses must override and rewrite all abstract methods in the abstract parent class.
Covering rewriting (implementation): The subclass removes the abstract keyword of the abstract method, and then does not put curly braces on the method body.
4. Create a subclass object for use

An abstract class does not necessarily have an abstract method.
As long as the class in which the abstract method is located is an abstract class, that is enough.
Such an abstract class without abstract methods cannot directly create objects, which is useful in some special scenarios.

**

polymorphism

**
extends inheritance or implement implementation is the premise of polymorphism
Polymorphism is reflected in the code, in fact, it is a sentence, the parent class reference points to the subclass object
format:
parent class name object name = new subclass name ();
or:
Interface name object name = new implementation class name ();
using polymorphism
The reference of the parent class on the left points to the object of the subclass on the right.
The usage characteristics of member variables in polymorphism
Two ways to access member variables:
1. Access member variables directly through the object name: see who is on the left of the equal sign, whoever is used first, and look up if there is no one.
2. Access member variables indirectly through member methods: see who the method belongs to, who is the first to use, and if not, look up.

In polymorphic code, the access rules of member methods are:
look at who is new, use whoever is new first, and
look up
if not Member method
on the left
: compile to the left, run to the right

The advantage of polymorphism: No matter which subclass object is replaced when the right side is new, the calling method on the left side of the equal sign will not change

Upward transformation of objects
1, the upward transformation of objects is actually polymorphic writing:
format: parent class name object name = new subclass name (); Animal animal = new cat();
meaning: create a subclass object on the right, Treat him as a parent class.
Precautions: Upward transformation must be safe, from a small-scale to a large-scale, from a small-scale cat to an upward transformation into a larger-scale animal, it must be
safe and no problem, But there is also a disadvantage.
Once the object is transformed into the parent class, the original unique content of the subclass cannot be called.
Solution: Use the downward transformation of the object [Restore]

Downward transformation of an object
2. The downward transformation of an object is actually a [restore] action.
Format: subclass name object name = (subclass name) parent class object;
meaning: [restore] the parent class object to the original of subclass objects.
Animal animal = new cat();//It was originally a cat, and it has been transformed into an animal
Cat cat = (cat)animal;//It was originally a cat, but it has been regarded as an animal, and it has been restored to become the original cat
Note:
A, must ensure The object was originally a cat when it was created, so it can be downcast to cat
B. If the object was not a cat when it was created, but now it must be downcast to a cat, an error will be reported. An error classCastException is thrown


How can I know what subclass the
object referenced by a parent class is originally
?

Guess you like

Origin blog.csdn.net/qq_48627750/article/details/120382537