Java Polymorphism - Encapsulation and Class Method Overloading

   Encapsulation is to enclose the process and data, and the access to data can only be encapsulated through the keywords private, protected and public in java through the defined interface method.


   Encapsulation combines all the components of an object. Encapsulation defines how the program refers to the data of the object. Encapsulation actually uses methods to hide the data of the class and control the extent to which users can modify the class and access data. Proper encapsulation can make the code easier to understand and maintain, and also enhance the security of the code. Overload is the behavior in the same class, which is different from the override (behavior between different classes) introduced in the previous issue.


   Here is a simple example to illustrate the application of overload.


   Overloading of constructor with different parameters


insert image description here


   When the same method name does not take parameters, it does not conform to the rules of overload, and an error will be reported.


insert image description here


   If the above method name is changed to one of the parameters, it can comply with the overload rule.


insert image description here


insert image description here


insert image description here


   Through the simple examples above, you can see the overloaded encapsulation of overload, and create different methods according to the needs to achieve the results of different methods. The next issue will continue to introduce polymorphic abstract method implementations.

Guess you like

Origin blog.csdn.net/weixin_48591974/article/details/127417723