Java serialization 47 polymorphism basic syntax, function

1. Polymorphic grammar

1. If there is no inheritance relationship between two classes, polymorphism cannot be used for compilation.

2. No matter upward or upward transformation, inheritance is needed.

3. When is the downward transformation needed?

When the called method or property is specific to a subtype and does not exist in the parent type, a downward transformation is required.

4. Famous exception: java.lang.ClassCastException

 

package com.bjpowernode.java_learning;

​

public class D47_ {

  public static void main(String[] args) {

    Animal46 a3 = new Cat46();

    //a3.catchMouse();//The method used here is Animal46 Does not exist in, but in Cat46 Exists in, so cast is required

    Cat46 c4 = (Cat46)a3;

   

    Animal46 a5 = new Bird46();

    /**Cat46 c5 = (Cat46)a5;

    *

     * There is no problem in compiling the above program, because the compiler has checked that the data type of a5 is animal46, which is between animal46 and Cat46

     * In addition, Animal46 is the parent type, Cat46 is the child type, and the conversion of the parent type to the child type is called the down conversion type. The syntax is legal,

     * Although the program has been compiled, there will be exceptions when the program is running, because the object that really exists in the JVM heap memory is Bird type, Bird

     * The object cannot be converted to a Cat object because there is no inheritance between the two types and a well-known exception occurs:

     * java.lang.ClassCastException

     * Type conversion exception, which always occurs during "down transition".

     *It can be modified as follows:

     *if(a5 instanceof Cat46){

     *  Cat46 c5 = (Cat46) a5;}

     *else if(a3 instanceof Bird46){

     *  Bird46 b5 = (Bird46) a5;}

     */

   

   

  }

}

class Animal46{

  public void move() {

    System.out.println("Animals walk again");

  }

 

}

class Cat46 extends Animal46{

  public void move() {

    System.out.println("Kitten walking");

  }//Rewrite function

  //Here are the cat specific functions

  public void catchMouse() {

    System.out.println("Kittens catch mice");

  }

}

class Bird46 extends Animal46{

  public void move() {

    System.out.println("Birds are flying");

  }

}

 

Be careful:

(1) the above exceptions will only appear when the type conversion is enforced, that is to say, there is a hidden danger in "downward transformation" (the compilation is passed, and then the operation is wrong)

(2) in the upward transformation, as long as the compilation is passed, there will be no error in running. Animal46 a1 = new Cat46()

(3) downward transformation may lead to errors in operation: Animal46 a1 = new Cat45();Bird46 b1 = (Bird46)a1;

5. How to avoid ClassCastException during downward transformation?

Using the instanceof operator can avoid the above situation. How to use it?

(1) format: the data type after this is the actual data type stored in the heap memory.

(reference to instanceof data type name)

(2) the execution result of the above operators is boolean type, and the result may be true/false

(3) about the operation result true/false

Hypothesis: (a instanceof Animal)

true means: a the object that this reference points to is an Animal type

false means that the object a refers to is not an Animal type

6. The Java specification requires: before the cast, it is recommended to use the instanceof operator to judge to avoid ClassCastException exception exception.

II. Code debugging

 


Shortcut keys:

F5: enter into the program

F6: next step

F7: end of current method

F8: execute next breakpoint


4. The role of polymorphism

1. The role of polymorphism in the actual development is illustrated by the following example of pet feeding by the owner:

(1) analysis: in order to realize the scenario of owner feeding pets, type abstraction is needed:

Master (class)

The owner can feed the pet, so the owner has the action of feeding

Pets (category)

Pets can eat, so pets have the action of eating

(2) the core of object-oriented programming: define the class, instantiate the class as an object, drive the environment, and let the objects cooperate to form a system

(3) the role of polymorphism

Let's start with an example of not using polymorphism

 

package com.bjpowernode.java_learning;

​

public class D47_2_EffectOfPolymorphic {

  public static void main(String[] args) {

    Master47 m1 = new Master47();

    Cat47 c1 = new Cat47();

    m1.feed(c1);

    Dog47 d1 = new Dog47();

    m1.feed(d1);

   

  }

​

}

class Cat47{

  public void eat() {

    System.out.println("Kittens love fish");

  }

}

class Dog47{

  public void eat() {

    System.out.println("Dogs love to eat bones");

  }

}

​

class Master47{

  public void feed(Cat47 c) {

    c.eat();

  }

  //Function overload. If you have a dog, you have to call

  public void feed(Dog47 d) {

    d.eat();

  }

}

 

From the above example, we can see that this method does not use the polymorphism mechanism in java language, and the disadvantage is that the expansion of Master47 is very poor, because as long as a new pet is added, Maset47 class needs to add new methods.

We add several concepts:

Improve the coupling degree (decoupling) and the expansion force (a very important goal of software development)

Our daily line v: Abstract oriented programming should not be oriented to specific process programming. The advantages of abstract oriented programming are low coupling and strong extension ability.

 

package com.bjpowernode.java_learning;

​

public class D47_3_PolymorphicExample {

  public static void main(String[] args) {

    Pet pDog = new Dog47_2();

    Pet pCat = new Cat47_2();

    Master47_2 m2 = new Master47_2();

    m2.feed(pDog);

    m2.feed(pCat);

    //We can take advantage of polymorphism to keep pets as our parents

  }

​

}

class Pet {

  public void eat() {  }

}

class Cat47_2 extends Pet {

  public void eat() {

    System.out.println("The kitten is still eating fish");

  }

}

class Dog47_2 extends Pet {

  public void eat() {

    System.out.println("The dog is still eating bones");

  }

}

class Master47_2{

  public void feed(Pet p) {

    p.eat();

  }

}

V. source code:

D47_1_polymorphic.java

D47_2_EffectOfPolymorphic.java

D47_3_PolymorphicExample.java

Address:

https://github.com/ruigege66/Java/blob/master/D47_1_polymorphic.java

https://github.com/ruigege66/Java/blob/master/D47_2_EffectOfPolymorphic.java

https://github.com/ruigege66/Java/blob/master/D47_3_PolymorphicExample.java

2.CSDN: https://blog.csdn.net/weixin_

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to pay attention to WeChat public number: Fourier transform, personal public number, only for learning exchanges, background reply "gift package", access to big data learning materials.

 

Keywords: Java Programming github jvm

Added by mrmachoman on Sun, 03 Nov 2019 01:23:02 +0200