Knowledge you should know in inheritance and polymorphism

Object-Oriented Programming

1. Inheritance:

Sometimes there are some associations between objective things, then there will also be some associations when expressed as classes and objects.
For example, a cat is an animal, and it has the basic attributes of an animal. When we define the cat class, we don’t need to re-define some of the animal attributes, just let the cat class inherit the animal class.
(Code reuse reduces code redundancy)

1. Form of expression: A extends B
A: subclass, derived class
B: parent class, base class, super class

2. What does the subclass inherit from the parent class? (BAT Interview)
Everything except the construction method! ! !
(The subclass needs to help the parent class construction method, that is, access the parent class construction method)

3. The difference between this and super keywords? (Interview)
(1) This represents the reference of the current object
https://blog.csdn.net/qq_45658339/article/details/108900917
super represents the reference of the parent class
(2)
Insert picture description here

4. Protected keyword:
We found that if the field is set to private, the subclass cannot be accessed. But setting it to public violates our original intention of "encapsulation", so we introduced the protected keyword

There are four access rights for fields and methods in Java:
(1) private: access inside the class, but not outside the class (private)
(2) default (also called package access default): access inside the class, the same Classes in the package can be accessed, other classes cannot be accessed
(3) protected: the class can be accessed, subclasses and classes in the same package can be accessed, other classes cannot be accessed. (protected)
(4) public: class internal And class callers can access (public)
Insert picture description here

5. The difference between rewriting and reloading? (Interview)
https://blog.csdn.net/qq_45658339/article/details/108822052

6. The usage of final:
(1) Final can modify variables (become constants ), and can only be initialized once
final int SIZE = 10;
(2) Final can modify the class , meaning that the current class cannot be inherited (final class Animal)
(3 ) ) Final can modify the method , the currently modified method cannot be overridden

2. Polymorphism:

Polymorphism: actually a kind of thought

From the code level: the parent class refers to the subclass object, and the parent class and the subclass have the same-named override method (override). When the override method of the same name is invoked through the reference of the parent class, he may show Different behaviors, the process of showing such different behaviors, this kind of thinking is called polymorphism.

Prior knowledge:
Insert picture description here

Up-casting : reference of parent class, reference to subclass object
① direct assignment ② parameter passing of method ③ return value

Dynamic binding :
1. The reference of the parent class, refer to the object of the subclass (upcast)
2. Through the reference of the parent class, call the method rewritten by the subclass and the parent class
(the construction method will also be dynamically bound)

Examples of polymorphism:

class Shape {
    
    
    public void draw() {
    
    
    }
}
class Cycle extends Shape {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("画一个⚪圈!");
    }
}
class Rect extends Shape {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("画一个♦");
    }
}

public class oo2 {
    
    
    public static void drawMap(Shape shape) {
    
    
        shape.draw(); //多态的核心
    }

    public static void main(String[] args) {
    
    
        Shape shape1 = new Cycle();
        Shape shape2 = new Rect();
        drawMap(shape1);
        drawMap(shape2);
    }
}

operation result:
Insert picture description here

Benefits of using polymorphism:

1. The cost of using the class by class callers is further reduced.
Encapsulation means that the caller of the class does not need to know the implementation details of the class. Polymorphism allows the caller of the class to not even know what the type of the class is, but only needs to know This object can have a certain method.
Therefore, polymorphism can be understood as a further step of encapsulation, allowing class callers to reduce the cost of using the class

2. It can reduce the "cyclocomplexity" of the code and avoid using a large number of if-else
(cyclocomplexity: the number of occurrences of conditional statements and loop statements in a piece of code )

3. Stronger scalability

Guess you like

Origin blog.csdn.net/qq_45658339/article/details/109008486