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.

 

         向上转型:在多态中需要将子类的引用赋给父类对象,只有这样该引用才能够具备技能调用父类的方法和子类的方法。

 对于Java而言,它多态的实现机制遵循一个原则:当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法。

2.2实现形式

      在Java中有两种形式可以实现多态。继承和接口。

      2.2.1、基于继承实现的多态

      基于继承的实现机制主要表现在父类和继承该父类的一个或多个子类对某些方法的重写,多个子类对同一方法的重写可以表现出不同的行为。

 

      2.2.2、基于接口实现的多态

 

      继承是通过重写父类的同一方法的几个不同子类来体现的,那么就可就是通过实现接口并覆盖接口中同一方法的几不同的类体现的。

 

      在接口的多态中,指向接口的引用必须是指定这实现了该接口的一个类的实例程序,在运行时,根据对象引用的实际类型来执行对应的方法。

 

      继承都是单继承,只能为一组相关的类提供一致的服务接口。但是接口可以是多继承多实现,它能够利用一组相关或者不相关的接口进行组合与扩充,能够对外提供一致的服务接口。所以它相对于继承来说有更好的灵活性。

所以多态机制遵循的原则概括为:当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法,但是它仍然要根据继承链中方法调用的优先级来确认方法,该优先级为: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=324966666&siteId=291194637