Inheritance is used to reuse parent code. Inheritance can be used if two classes have an IS-A relationship. At the same time, inheritance also pave the way for the realization of polymorphism.

polymorphism

Polymorphism refers to procedures defined in the reference variable is pointing to the specific type and referenced by the variable from method calls are not sure when programming, but during the program is run to determine, that is, a reference variable STLL can point to which class instance object, the reference variable from method calls the method to realize exactly is which class, It must be decided during the running of the program. Because the program is running to identify a specific class, so, don’t need to modify the source code, can make reference variables bound to the different class implements, leading to the reference call specific methods change, namely do not modify the program code can change the program of the specific binding in the runtime code, let the program can select multiple running state, this is polymorphism.

Polymorphism, in short, is the ability to have many different manifestations or forms of the same behavior. For example, if I have a glass of water, I don’t know if it’s warm, cold or hot, but I know when I touch it. When I touch the cup, I get different results for different temperatures of water. That’s polymorphism.

Conditions for polymorphism

  1. Inheritance. There must be a subclass and a parent class that have an inherited relationship in polymorphism.
  2. Rewrite. A subclass redefines the methods of its parent class so that the subclass’s methods are called when those methods are called.
  3. Transition up. In polymorphism, a reference to a subclass needs to be assigned to a superclass object so that the reference has the ability to call both the methods of the parent class and the methods of the subclass.

For Java, the implementation mechanism of polymorphic follow one principle: when the superclass object reference variable reference subclass object, is the type of the reference object rather than a reference variable type whose members decided the call method, but this is the method called must be defined in the parent class, which is covered by a subclass.

upcasting

An upcast occurs when an object referenced by a subclass is converted to a superclass type. In layman’s terms, a subclass object is converted into a superclass object. Here the superclass object can be an interface.

public class Animal {
public void eat(){
System.out.println("animal eatting...");
}
}

public class Cat extends Animal{

    public void eat(){

        System.out.println("Cats eat fish.");
    }
}
public class Dog extends Animal{

public void eat(){
System.out.println("Dogs eat bones.");
}

public void run(){
System.out.println("I can run."); } } public class Main { public static void main(String[] args) { animal = new Dog(); // Animal.eat (); }}Copy the code

Output: Dogs eat bonesCopy the code

Animal Animal = new Dog(); Convert Dog, a subclass object, to Animal. In this case the animal reference calls a subclass method.

Problems needing attention in the process of transformation

  • Methods defined separately by subclasses are lost when you transition up. For example, if an animal reference points to an instance of Dog, the run method cannot be accessed.animal.run()Complains.
  • Subclass references cannot point to superclass objects.Dog d = (Dog)new Animal()That’s not going to work.

The benefits of upward transition

  • Reduce repetitive code and simplify code.
  • Improve system scalability.

Downward transition (with pits)

The opposite of upward transformation is downward transformation. A downward transformation is to turn a parent object into a subclass object. Attention, please! There are pits.)

A = new cat (); Cat c = ((Cat) a); c.eat(); // I eat fish d = ((Dog) a); d.eat(); / / error: Java. Lang. ClassCastException: com.chengfan.animal.Cat cannot be cast to com.chengfan.animal.Dog Animal a1 = new Animal(); Cat c1 = ((Cat) a1); c1.eat(); / / error: Java. Lang. ClassCastException: com. Chengfan. Animal. The animal always be cast to com. Chengfan. Animal. The CatCopy the code

Why doesn’t the first code report an error? Because A itself is a Cat object, so of course it can be converted to Cat, and because it’s Cat, it can’t be converted to Dog.

While A1 is an Anmail object, it cannot downcast any subclass of Wie. Like finding a fossil and knowing it’s an animal, but you can’t just say it’s a cat or it’s a dog.

Notes for downward transition

  • A downward transition requires that the superclass object refers to a subclass object (that is, it must be upward before it can be downward)
  • A downward transition can only turn into this class of objects (cats cannot turn into dogs).

The instanceof keyword: Instanceof is a binary Java and PHP operator (operator) in the same class as ==, >, and <. Because it is composed of letters, it is also a Reserved Java keyword. It determines whether the object on its left is an instance of the class on its right and returns Boolean data. Can be used to determine whether an instance of an inherited subclass is an implementation of the parent class.

Instanceof usually precedes type conversions to avoid exceptions!

Public void eat(Animal a){/* * Subclass refers to an instance (object) of the parent class, which must be used to cast * the instance can call subclass-specific methods * Must meet the transformation condition to convert (), Subclasses cannot cast each other arbitrarily, but a subclass reference to a parent instance can cast the * instanceof operator: returntrue/false, * /if(a instanceof Dog){ Dog d = (Dog)a; d.eat(); d.run(); // Dogs have a way of running}if(a instanceof Cat){  
        Cat c = (Cat)a;
        c.eat();
        System.out.println("I wish I could run, but I can't."); } a.at (); } eat(new Cat()); eat(new Cat()); eat(new Dog());Copy the code

Hua Mulan joined the army in her father’s place

Hua Mulan joins the army for her father, Hua Arc. So at this time Hua Mulan is a subclass, hua Arc is the parent class. The flower arc has its own member attributes age, name, gender. Mulan also has these attributes, but they are obviously completely different. Flower arc has her own non-static method of ‘riding a horse to kill an enemy’, and Mulan also inherited the same method of ‘riding a horse to kill an enemy’ from her father. Hua Arc also has a static method of ‘self-introduction’, everyone can ask hua Arc what his name is. Mulan also has a unique non-static member method of her own: “Paint”. But now Hua Mulan joins the army in her father’s place, disguising herself as a man. This time is equivalent to the parent class reference the name (arc) point to a subclass object (hua mu LAN this person), then access on the rest of the class (and others) subclass objects (hua mu LAN this person) member attributes (name, age, gender, actually saw the hua mulan her father’s name (arc), age (60), gender (male). When accessing the non-static member method of a subclass object (Mulan the person), you see Mulan herself riding a horse to fight a war using her 18 martial arts. When visiting Hua Mulan’s static method (self-introduction), Hua Mulan herself introduces herself to others using her father’s name information. And at this time Mulan cannot use her own unique method of membership ‘paint’.

—– the upward transformation in polymorphism so finally a will be successful ten thousand bones withered, the battle started to win, Hua Mulan bid farewell to the war life. One day, met his beloved man, at this time the power of love will parent class object reference (flower arc this name) forced conversion to subclass object original reference (Hua Mulan this name), so Hua Mulan again become her own, at this time she is completely her own. Name is Hua Mulan, age is 28, gender is female, fighting is still as fierce female man, self-introduction is dignified to tell others my name is Hua Mulan. OMG!!! Finally, it was time to use its own unique member method of ‘painting’. Since then, Hua Mulan has completely returned to the hua Mulan she was before she joined the army for her father. And with their beloved man happy life. —– downward transition in polymorphism

Remember, the upward transformation downward transformation must be under the premise of polymorphism, otherwise forced to change the daughter into a father, or father into a woman, will become Oriental invincible, the system will report an error at this time illegal type conversion. Hahahaha. In addition, the development generally uses the polymorphic declaration formal parameters, and creates the anonymous object of the subclass as the actual parameters. The above.

Polymorphic member access features

  • Member variables compile to the left (parent), run to the left (parent)
  • Member methods compile to the left (parent class) and run to the right (subclass). Dynamic binding
  • Static methods compile to the left (superclass) and run to the left (superclass).

Static is class-specific, not overridden, so access is still left, only non-static member methods, compile to the left, run to the right


Classic Case Analysis

public class A {
    public String show(D obj) {
        return ("A and D");
    }

    public String show(A obj) {
        return ("A and A");
    } 

}

public class B extends A{
    public String show(B obj){
        return ("B and B");
    }
    
    public String show(A obj){
        return ("B and A"); } } public class C extends B{ } public class D extends B{ } public class Test { public static void main(String[] args) {  A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C(); D d = new D(); System.out.println("1--" + a1.show(b));
        System.out.println("2 --" + a1.show(c));
        System.out.println("3--" + a1.show(d));
        System.out.println("4--" + a2.show(b));
        System.out.println("5--" + a2.show(c));
        System.out.println("6--" + a2.show(d));
        System.out.println("7--" + b.show(b));
        System.out.println("8 --" + b.show(c));
        System.out.println("9--"+ b.show(d)); }}Copy the code
1--A and A
2--A and A
3--A and D
4--B and A
5--B and A
6--A and D
7--B and B
8--B and B
9--A and DCopy the code

Here are a few things to keep in mind when analyzing this problem

When a parent object references a variable that references a subclass object, the type of the referenced object determines whose member method is called, and the type of the referenced variable determines which method can be called. If the method is not overridden in a subclass, it is looked for in the parent class.Copy the code

Let’s start with an example

class X {
    public void show(Y y){
        System.out.println("x and y");
    }

    public void show(){
        System.out.println("only x");
    }
}

class Y extends X {
    public void show(Y y){
        System.out.println("y and y"); } public void show(int i){ } } class main{ public static void main(String[] args) { X x = new Y(); x.show(new Y()); x.show(); }} // result //y and y //only xCopy the code

Show (O), super.show(O), this.show((super)O), super.show((super)O).Copy the code

Y inherits from X, overriding the show (Y Y) method in X, but not the show () method.

In this case, X referring to type X refers to object Y, and in this case, the method to be called depends on Y, and will look for Y first. Perform x.s how (new Y ()); , the method is defined in Y, so the method in Y is executed;

But execute x.how (); And some people say, well, Y doesn’t have this method, right? It looks like it went to the parent class to find the method, because it called the method in X.

In fact, there is a show () method in class Y. This method inherits from X but does not override it, so it is not explicitly written in Y. It looks like it is calling a method in X, but it is actually calling a method in Y.

Now look at the above difficult sentence is not difficult to understand it. X is the reference variable type, which determines which methods can be called; Show () and show(Y Y) can be called, whereas show(int I) cannot. Y is the type of the referenced object that determines whose method is called: the method that calls Y.


The relationship of ABCD is as follows: C/D –> B –> A

  1. A1 is an instantiated object of class A, so this points to A, and then we look for this.show(b), and since there is no such method, we go to super.show (b), but since class A has no parent, we go to this.show(super b), and since b’s parent is A, So it equals this.show(A), and then finds the method in class A, and prints A and A.
  2. A1 is an instantiated object of class A, so this points to A, and then we look for the this.show(C) method in class A, so we go to super.show(C), and we look for the super.show(C) method in class A, but A doesn’t have A parent, so we go to this.show(super C), A = this.show(A); B = this.show(A); C = this.show(B);
  3. A1 is an instantiation object of class A, so this points to A, and then we find the this.show(D) method in class A, so we print A and D;
  4. A2 is A reference object to class B, and this is of type A, so this refers to class A, and then look for this. Show (B) method in class A, so super.show(B), because class A has no parent, so this.show(super B), B’s parent is A, Super B = A, so this is called. Show (A), in A way to find the show (A), find it, but because the a2 is A reference object, class B and class B covers the show of the class A (A) method, so it’s the execution is show in A class B (A) method, namely the output B and A;
  5. A2 is A reference object to class B, and this is of type A, so this refers to class A, and then look for this. Show (C) method in class A, so super.show (C) method, because class A has no parent, so this.show(super C) method in class C is B. So in A class for the show (B), also didn’t find it, found that B and the parent class, namely A, so you also continue to get the show in A class (A) method, find it, but because the a2 is A reference object, class B and class B covers the show of the class A (A) method, so it’s the execution is show in A class B (A) method, B and A;
  6. A2 is A reference object of class B, so this refers to class A, and then look for this. Show (D) method in class A, and then look for a2. Show (D) method in class B, and then look for a2. A and D;
  7. The prime minister executes this.show(b), finds the show(b) method in class B, and prints b and b;
  8. Super.show (C) = super.show(C) = super.show(C) = super.show(C) = super.show(C) = super.show(C) Show (super C) {this.show(super C) {this.show(super C);
  9. Super.show (D) = super.show(D) = super.show(D) = super.show(D) = super.show(D) = super.show(D) = super.show(D) = super.show(D) = super.show(D) = super.show(D)

conclusion

That’s about it for this article. So let’s sum it up.

  1. Polymorphism, in short, is the ability to have many different manifestations or forms of the same behavior.
  2. Classification of polymorphism: runtime polymorphism and compile-time polymorphism.
  3. The premise of runtime polymorphism is inheritance (implementation), rewriting, and upward transformation
  4. Upward transformation and downward transformation.
  5. Priority of calling object methods in inheritance chain: this.show(O), super.show(O), this.show((super)O), super.show((super)O).