This is the 22nd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

We all know that there are three major features in Java: inheritance, encapsulation, and polymorphism. So I’m going to talk about those today.

encapsulation

As always, ask yourself why these features exist. First, encapsulation, which uses permission modifiers to hide attributes and provides some common GET/set methods to access data. How does this achieve encapsulation? We can add some logical control statements inside the GET or set methods. For example, when setting the age, I limit the age passed in to no greater than 120. The code implementation could be written like this.

public void setAge(int age){ if(age > 120){ this.age = 120; }} public int getAge(){if(this.gender == "female "){system.out.println (" female is the age you can see?" ); return -1; } return this.age; }Copy the code

This is just a simple example, and the actual situation can be optimized. Let’s say we restrict age less than zero. So we can see that encapsulation of data has the following advantages: precise control over members and hiding implementation details, as well as the freedom to modify the internal implementation of methods.

inheritance

Just as a son inherits his wealth, a subclass can inherit properties and methods from its parent, but some do not, which brings us back to access modifiers. In Java, only single inheritance is allowed, that is, only one Lao tzu. How is inheritance implemented? Why inheritance? We identify the inherited parent class through the extends keyword. And inheritance is transitive, A inherits B, B inherits C, so C is also A parent of A. Superclasses are also called superclasses, and subclasses are also called base classes. Object is the superclass of all classes. When a method in a subclass is called with the same name as the parent class, the subclass’s method is executed, while the parent class’s method can be explicitly called with super.

The advantages of inheritance are obvious. It can increase the reusability of code, but we should not inherit for inheritance’s sake. We should also pay attention to the concept of inheritance and composition. And composition is the concept of “has a”, for example, our body has a leg. The above two sentences can design the following structure.

Class Animal {sleep(){system.print.out (" Animal sleep "); Class Cat extends Animal{sleep(){system.print. Out (" sleep "); Class Body{Leg Leg = new Leg();}} class Body{Leg Leg = new Leg(); }Copy the code

polymorphism

And the last one is polymorphism, again, what is polymorphism? What’s the use of polymorphism? As the name suggests, polymorphism is multiple states. Just like the same morning when you are energized and the same morning when you are depressed, we are likely to perform the same method differently when we call it. This is called polymorphism in Java. So why should it be any different? Remember that when a subclass inherits a method from its parent, it can override it, and when we call the overridden method, the result is not the same as the result of the overridden method. So if you want polymorphism, you need inheritance. There is also a polymorphic form, which we call a “virtual method call” or an upward transition, where the parent class refers to a subclass object. So like this, the parent Animal’s A refers to the Cat entity.

Animal a = new Cat(); A.sleep () // The cat sleepsCopy the code

It is important to note that you cannot call a method unique to the parent class when using a virtual method call. This will give an error at run time, but will be fine at compile time, when only the left side of the equals sign will be compiled. However, when running, it will look at the specific object, and it will report an error if it cannot find the corresponding method in the subclass.

conclusion

We can sum up the role of polymorphism, we can use polymorphism to achieve decoupling and easy to expand the purpose, we write different module function, in the call time only need to change the parent class reference direction. You just need to provide a stable subclass implementation, with the parent class only responsible for calling the corresponding interface method. This is how the factory design pattern works.