The three basic characteristics of object-oriented are: encapsulation, inheritance, and polymorphism.
Encapsulation can hide implementation details and make code modular for code reuse.
Inheritance can extend existing code modules (classes), and the purpose is also for code reuse.
Polymorphism is to achieve interface reuse! The role of polymorphism is to ensure the correct call when using a certain attribute of an instance of any class in the "family tree" when inheriting and deriving classes.
Encapsulation
1. Definition of package
Encapsulation literally means packaging. The professional point is information hiding, which refers to the use of abstract data types to encapsulate data and data-based operations to form an indivisible independent entity. Data is protected in abstract data types. Inside, hide the details of the interior as much as possible, and only keep some external interfaces to connect with the outside. Other objects of the system can only communicate and interact with this encapsulated object through authorized operations wrapped outside the data. That is to say, the user does not need to know the internal details of the object, but can access the object through the external interface provided by the object.
For encapsulation, an object encapsulates its own properties and methods, so it can complete its own operations without relying on other objects.
2. Advantages of packaging:
1. Hide implementation details.
2. Security.
3. Increase code reusability.
4. Modular.
3. Use of package
1. Use the private modifier to indicate the minimum access rights.
2. To access member variables, uniformly provide setXXX and getXXX methods
Inheritance
1. What is inheritance
Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits the methods from the parent class, so that the subclass has the same behavior as the parent class. Of course, if you have a private attribute (private modification) in the parent class, the subclass cannot be inherited.
2. Characteristics of Inheritance
1. Improve code reusability.
2. The attribute method of the parent class can be used in the subclass.
3. You can easily define subclasses.
4. Make designing applications easy.
3. The use of inheritance
1. In the parent-child class relationship inheritance, if the member variable has the same name, there are two ways to access when creating a subclass object.
a. Access member variables directly through subclass objects
Whoever is on the left of the equal sign will be used first, and if there is no one, look up.
b. Access member variables indirectly through member methods
Whoever the method belongs to will use it first, and if not, look up.
4. Precautions for inheritance
1. When the parent class changes, the subclass must change.
2. Inheritance destroys encapsulation. For the parent class, its implementation details are transparent to the subclass.
3. Inheritance is a strong coupling relationship.
Polymorphism
1. What is polymorphism
Polymorphism is the ability to have multiple different manifestations or morphologies of the same behavior.
2. The characteristics of polymorphism
1. Eliminate the coupling relationship between types and achieve low coupling.
2. Flexibility.
3. Scalability.
4. Replaceability.
3. The embodiment of polymorphism
1. Inheritance
2. The parent class reference points to the subclass
3. Rewrite
4. Upward Transformation
parent class name object name = new subclass name();