encapsulation, integration, polymorphism, and abstraction are fundamental features of Java.
The first step in encapsulation is to assemble the class, which is to define a class, and consider the properties, methods, etc. of the class. The second step is the hiding of information, which includes access modifiers, Get/set methods, and implementations of certain methods. When using the private modifier to provide the Get/set method, there are some advantages:
1, can only provide get or set method, the property becomes read-only or write-only;
2, the Get method can provide authentication of permissions;
3. The Set method can provide validation of the validity of the data;
4, can hide the internal properties of the storage mode;
Polymorphism refers to the same behavior, different implementations. It is also divided into static polymorphism and dynamic polymorphism, in which dynamic polymorphism is the essence of Java object-oriented. Static polymorphism is implemented by overloading of methods, dynamic polymorphism is realized by means of rewriting and dynamic binding, and dynamic polymorphism must occur in inheriting class relationships.
The technical basis for polymorphism is as follows:
1, Upward transformation Technology: A parent class reference can point to different sub-class objects;
2, instanceof keyword: used to judge the real type of the runtime object, is to ensure that the code runs without the crash of the important barrier;
3. Dynamic Binding Technology: If Class A is to invoke Class B objects, the Class B's parent class should be bound as much as possible. If the method of the parent class is overridden in a subclass, the method that is overridden in the subclass is executed when executing the program, rather than calling the method in the parent class, which increases the richness. The overridden method in the parent class is actually hidden.
Java encapsulation and polymorphism