Hello, everyone. Last time we talked about inheritance in Java. This time we will talk about polymorphism in Java. Stop gossiping and return to the truth. Let’s Talk Android!

Ladies and gentlemen, today we are introducing the essence of object-oriented knowledge: polymorphism. Polymorphism is represented by the conversion of a subclass object to a superclass object and the call of a subclass object’s methods through the superclass object. It’s certainly very abstract to say that, and again, we’re using pseudocode to demonstrate polymorphism.

class A
{
    permission type valA; 
    permission func1(a)
    { 
        //do something }};class B extends A
{
    permission type valB;
    permission func1(a)
    { 
        //do something 
    };
    permission func2(a)
    { 
        //do something }}; A objA =new A(); 
B objB = new B(); 

objA.func1();   // call the func1 of A 
objB.func1();   // call the func1 of B
objA = objB;   // change the type of class, but can't do this: objB = objA;
objA.func1();   // call the func1 of B
objA.func2();    // can't call the func2 of B
Copy the code

In the above code, class B inherits from class A. In addition to the member variables and functions of class A, it also defines its own member variable valB and member function func2. It also defines A member function func1. From an inheritance point of view, it already inherits func1 from class A. If it defines func1, then it has two member functions func1. No, it has only one member function func1. According to the syntax rules in Java, if func1 of class A is an abstract function, then this behavior of class B is called implementation; If func1 of class A is not abstract, then this is called overwriting or overwriting. In our pseudocode, it is clear that class B overrides the func1 method of class A. Overwriting allows us to call the method func1 of class B with an object of class A, objA, which is polymorphic. We can also see from the pseudocode that an object of class A cannot call the method func2 of class B without an override method.

The important thing to note about overwriting is that the function prototypes of the subclass and its parent must be exactly the same in order to count as a subclass overwriting the method of the parent class. If the function prototypes are not the same, then it is not overwritten. So what is it? At this time the officer said: overload. That’s right, reloading. This is cross-class function overloading. This type of function overloading cuts across a class’s inheritance architecture and is less obvious across classes than overloading in the same class. It seems that the judge who just answered has a pair of bright eyes, ha ha.

Looking at the pseudocode above, objB of class B can be converted to objA of class A, which is also polymorphic, and vice versa. However, if an object of class A is converted from an object of class B, it can be converted back to its original type. If objA = objB is executed first, then objB = objA is executed. Type conversions are prone to errors, so I recommend using the instanceof keyword before converting, which determines the type of an object. It can be used as follows:

object instanceofClassName; If the type of object is ClassName, it returnstrueOtherwise returnfalse.Copy the code

We combine the above pseudocode to write the following pseudocode:

A objA = new A(); 
B objB = new B(); 
objA instanceof A;  //it return true 
objB instanceof B;  //it return true
objA instanceof B;  //it return false 
objB instanceof A;  //it return false 
objA = objB;        // change the type of class
objA instanceof A;  //it return true 
objA instanceof B;  //it return true
Copy the code

As you can see from the pseudocode above, the instance keyword can be used to determine the type of an object, thus creating safe prerequisites for converting objects of different types. Of course, this only applies to Class types; other basic types, such as ints, do not.

Look officers, polymorphism is the face of the object knowledge is more important and difficult to understand the knowledge, I hope we provide a combination of pseudo-code to think, so that it will be easy to understand some, but also can put the abstract knowledge into concrete knowledge, in the future when writing code will also use.

Everyone see officer, on the Java polymorphic example let’s introduce here, want to know what other examples, and listen to the next decomposition!