Java basics 11 -- polymorphism, java -- Polymorphism

Source: Internet
Author: User

Java basics 11 -- polymorphism, java -- Polymorphism
Java basics 11-polymorphism 1. Definitions of Polymorphism

Simply put: an object corresponds to different types.

The embodiment of polymorphism in code:
The reference of the parent class or interface points to the object of its subclass.

1/* 2 3 object polymorphism. 4 5 class animal 6 {} 7 8 class cat extends animal 9 {} 10 11 class dog extends animal 12 {} 13 14 15 16 Cat x = new CAT (); 17 18 animals x = new CAT (); // an object in two forms. 19 20 21 22 cats and other things have the form of human cats and animals. 23. This is the object polymorphism. 24 25 Simply put: an object corresponds to different types. 26 27 polymorphism in the Code: 28 reference of the parent class or interface points to the object of its subclass. Benefits of 29 30 31 polymorphism: 32 improves code scalability and can be used for pre-defined code. Disadvantages of 33 34 polymorphism: 35 pre-defined content cannot use (called) Post-subclass special content. This can be solved through a downward transformation. 36 37 polymorphism premise: 38 1, must be related, inheritance, implementation. (Implementation is a special inheritance.) 39 2 must be covered. 40 41 42 43 */44 45 abstract class Animal 46 {47 abstract void eat (); 48 49} 50 51 class Dog extends Animal 52 {53 void eat () 54 {55 System. out. println ("chew bone"); 56} 57 void lookHome () 58 {59 System. out. println (""); 60} 61} 62 63 class Cat extends Animal 64 {65 void eat () 66 {67 System. out. println ("fish"); 68} 69 void catchMouse () 70 {71 System. out. println ("Mouse capture"); 72} 73} 74 75 class Pig extends Animal 76 {77 void eat () 78 {79 System. out. println ("Feed"); 80} 81 void gongDi () 82 {83 System. out. println (""); 84} 85} 86 87 88 89 class DuoTaiDemo 90 {91 public static void main (String [] args) 92 {93 94 // Cat c = new Cat (); 95 // c. eat (); 96 // c. catchMouse (); 97 98 Animal a = new Cat (); // The automatic type is increased, and the Cat object is elevated to the Animal type. However, the special function cannot be accessed by s. 99 // restrict access to special functions. 100 // Major: upward transformation. Hide the child type. You do not need to use the special method of subclass. 101 102 103 // a. eat (); 104 105 // if you want to use the specific features of the animal cat. 106 // you can perform a downward transformation on this object. 107 // Cat c = (Cat) a; // The purpose of the downward transformation is to use the unique method in the subclass. 108 // c. eat (); 109 // c. catchMouse (); 110 111 // Note: For transformation, the type of the subclass object is changing from the beginning to the end. 112 // Animal a1 = new Dog (); 113 // Cat c1 = (Cat) a1; // ClassCastException114 115 116/* 117 Cat c = new Cat (); 118 119 // Dog d = new Dog (); 120 121 // c. eat (); 122 method (c); 123 // method (d); 124 // method (new Pig ()); 125 */126 127 method (new Dog (); 128 129} 130 131 public static void method (Animal a) // Animal a = new Dog (); 132 {133. eat (); 134 // when the type matching problem is solved, we can judge 135 if (a instanceof Cat) // instanceof: Used to determine the object type. It can only be used to reference the data type judgment 136 // usually used for robustness judgment before the downward transformation. 137 138 {139 Cat c = (Cat) a; 140 c. catchMouse (); 141} 142 else if (a instanceof Dog) 143 {144 Dog d = (Dog) a; 145 d. lookHome (); 146} 147 else148 {149 150} 151 152/* 153 public static void method (Cat c) 154 {155 c. eat (); 157} 158 public static void method (Dog d) 159 {160 161} 162 */163}

Upward transition: the parent class references the Child class object. The special functions of the subclass cannot be accessed.

Downward Transformation: subclass references the parent class object.

1 Animal a = new Cat (); // The automatic type is increased, and the Cat object is elevated to the Animal type. However, the special function cannot be accessed by s. 2 // restrict access to special functions. 3 // Major: upward transformation. Hide the child type. You do not need to use the special method of subclass. 4 5 Cat c = (Cat) a; // The purpose of the downward transformation is to use the unique method in the subclass.

In practical applications, the upward transformation facilitates code extension (the Code previously written can be used in the future, and only the inheritance or implementation of the base class is required). However, when the unique function of the subclass is used, downward transformation is required.

Most of the time, we transform to an Object class. When we use our own special functions, we turn back to the next step.

 

2. multi-State living examples
1/* 2 the story of Miss Bi and Grandpa bi. 3 */4 5 class 6 {7 void lecture () 8 {9 System. out. println ("management"); 10} 11 void phishing () 12 {13 System. out. println ("phishing"); 14} 15} 16 17 class instructor Bi extends Bi Grandpa 18 {19 void lecture () 20 {21 System. out. println ("Java"); 22} 23 void movie () 24 {25 System. out. println ("movie"); 26} 27} 28 29 30 31 32 33 class DuoTaiDemo234 {35 public static void main (String [] args) 36 {// original 37 // instructor Bi x = new instructor Bi (); 38 // x. lecture (); 39 // x. watching movies (); 40 // polymorphism 41 Grandpa Bi x = new instructor Bi (); 42 x. lecture (); // here we talk about Java content. Java covers the management field 43 x. phishing (); 44 45; Instructor y = (Instructor B) x; // ClassCastException46 y. watching movies (); 47 48 49 50 51} 52}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.