Three major features of Java (encapsulation, inheritance, polymorphism)

One of the three major characteristics - packaging

 For encapsulation, an object encapsulates its own properties and methods, so it can complete its own operations without relying on other objects.

      There are three major benefits of using encapsulation:

         1. Good packaging can reduce coupling.

         2. The internal structure of the class can be freely modified.

         3. More precise control over members.

         4. Hide information and realize details.

 

One of the three characteristics - inheritance

Inheritance defines how classes relate to each other and share characteristics. For several identical or acquainted classes, we can abstract their common behaviors or attributes and define them as a parent class or superclass, and then use these classes to inherit the parent class, they can not only have the properties and methods of the parent class You can also define your own unique properties or methods.

      At the same time, there are three things to remember when using inheritance:

         1. The subclass has non-private properties and methods of the parent class.

         2. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

        3. Subclasses can implement the methods of the superclass in their own way.

The above mentioned the many benefits of inheritance, so can we use inheritance recklessly? A word for you: Use inheritance with caution .

      First of all, we need to make it clear that inheritance has the following defects:

         1. When the parent class changes, the child class 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 .     

      So when we use inheritance, we need to be sure that using inheritance is indeed a valid and feasible way. So should we use inheritance? "Think in java" provides the solution: ask yourself if you need to upcast from subclass to superclass. If you must upcast, inheritance is necessary, but if you don't, you should consider whether you need inheritance .

 

One of the three characteristics - polymorphism

 

The so-called polymorphism means that the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable are not determined during programming, but are determined during the running of the program, that is, which reference variable will point to. The instance object of the class, the method in which the method call issued by the reference variable is the method implemented in the class must be determined during the running of the program. Because the specific class is determined when the program is running, the reference variable can be bound to various class implementations without modifying the source code, so that the specific method called by the reference changes accordingly, that is, the reference variable is not modified. The program code can change the specific code bound when the program runs, so that the program can choose multiple running states, which is polymorphism.

 

So for polymorphism we can summarize as follows:

 

      The parent class reference pointing to the subclass can only access the methods and properties owned by the parent class due to upcasting, and for methods that exist in the subclass but do not exist in the parent class, the reference cannot be used, although it is heavy. load this method. If the subclass overrides some methods in the parent class, when calling these methods, the methods defined in the subclass must be used (dynamic connection, dynamic call).

 

      For object-oriented only, polymorphism is divided into compile-time polymorphism and run-time polymorphism. Among them, polymorphism is static during editing, which mainly refers to the overloading of methods. It distinguishes different functions according to the different parameter lists. After editing, it will become two different functions, which is not polymorphic at runtime. . And runtime polymorphism is dynamic, it is achieved through dynamic binding, which is what we call polymorphism.

 

There are three necessary conditions for Java to implement polymorphism: inheritance, rewriting, and upcasting.

 

         Inheritance: In polymorphism there must be subclasses and superclasses that have an inheritance relationship.

 

         Overriding: The subclass redefines some methods in the parent class, and when these methods are called, the methods of the subclass are called.

 

         Upcasting: In polymorphism, the reference of the subclass needs to be assigned to the object of the superclass, only in this way can the reference have the skills to call the methods of the superclass and the methods of the subclass.

 For Java, its implementation mechanism of polymorphism follows a principle: when a superclass object references a variable that references a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member method is called, but this called The method must be defined in the superclass, that is, the method overridden by the subclass.

2.2 Realization form

      There are two forms of polymorphism in Java. Inheritance and Interfaces.

      2.2.1. Polymorphism based on inheritance

      The implementation mechanism based on inheritance is mainly manifested in the overriding of certain methods by the parent class and one or more subclasses that inherit the parent class, and the overriding of the same method by multiple subclasses can show different behaviors.

 

      2.2.2. Polymorphism based on interface implementation

 

      Inheritance is manifested by several different subclasses that override the same method of the parent class, then it can be manifested by several different classes that implement the interface and override the same method in the interface.

 

      In interface polymorphism, a reference to an interface must be an instance program that specifies a class that implements the interface. At runtime, the corresponding method is executed according to the actual type of the object reference.

 

      Inheritance is single inheritance, which can only provide a consistent service interface for a group of related classes. However, the interface can be multi-inheritance and multi-implementation, which can be combined and expanded by a group of related or unrelated interfaces, and can provide a consistent service interface to the outside world. So it has better flexibility than inheritance.

Therefore, the principles followed by the polymorphism mechanism are summarized as follows: when a superclass object references a variable to a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member method is called, but the called method must be in the The method defined in the superclass, that is to say, the method overridden by the subclass, but it still needs to confirm the method according to the priority of the method call in the inheritance chain, the priority is: this.show(O), super.show(O ), this.show((super)O), super.show((super)O).

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016488&siteId=291194637