Java array, generic, set usage and comparison in polymorphism, java Polymorphism

Source: Internet
Author: User

Java array, generic, set usage and comparison in polymorphism, java Polymorphism

When we use arrays and generic sets, we will inevitably encounter polymorphism, or under what circumstances can we use parent arrays to reference child arrays (collections and generics?

Use of arrays in Polymorphism

Array references with element as parent type can point to array objects with element as child Type

When an array is called, the type of the array element is determined by the current reference.

When an array with the element of the parent type points to an array object with the element of the Child type, you cannot use array references of the parent type to add elements other than the child type and its lower level to the Set (errors may occur during compilation)

1/* Verification: 2 * animals and dogs can reference the same array. 3 * When animals references an array, the array element is of the Animal type. 4 * when dogs references an array, the array element is Dog type 5 */6 7 public class Animal {8 private void showClass () {9 System. out. println ("the array element is of the Animal type"); 10} 11 12 public static void main (String [] args) {13 // animals and dogs can reference the same Array 14 Dog [] dogs = {new Dog (), new Dog ()}; 15 Animal [] animals = dogs; 16 17 // when animals references an array, the array element is Animal type 18 animals [0]. showClass (); 19 // when dogs references an array, the array element is Dog type 20 dogs [0]. showClass (); 21 // elements other than the child type and its lower-level cannot be added to the set through array references of the parent type (errors may occur during compilation) 22 animals [0] = new Animal (); 23} 24 25} 26 27 class Dog extends Animal {28 public void showClass () {29 System. out. println ("the array element is of the Dog type"); 30} 31}

 

Usage of generics in Polymorphism

ArrayList <parent type> cannot reference ArrayList <child type>

1/* Verification: 2 * animals cannot reference the dogs array 3*4 */5 import java. util. *; 6 7 public class Animal {8 9 public static void main (String [] args) {10 // animals cannot reference dogs array 11 ArrayList <Dog> dogs = new ArrayList <Dog> (); 12 ArrayList <Animal> animals = dogs; /* Compilation failed */13} 14 15} 16 17 18 class Dog extends Animal {19 20}

 

Java introduces a10 million characters"-- ArrayList <? Extends object>
This type of set can reference any parameter that inherits and implements the parameter type (including the parameter type itself ).

1/* characters 2 * animals can reference the dogs set 3 * When animals reference the set, the set element is of the Dog type 4 * when the dogs references the set, set element is Dog type 5 * cannot be modified through animals 6 */7 import java. util. *; 8 9 public class Animal {10 public void showClass () {11 System. out. println ("the set element is of the Animal type"); 12} 13 14 public static void main (String [] args) {15 // animals can reference the dogs array 16 ArrayList <Dog> dogs = new ArrayList <Dog> (); 17 dogs. add (new Dog (); 18 ArrayList <? Extends Animal> animals = dogs; 19 // when animals references a set, the set element is Dog type 20 animals. get (0 ). showClass (); 21 // when the dogs references the set, the set element is of the Dog type 22 dogs. get (0 ). showClass (); 23 // The set cannot be modified through animals (compilation fails) 24 // animals. add (new Dog (); 25 26} 27 28} 29 30 31 class Dog extends Animal {32 public void showClass () {33 System. out. println ("set element is Dog type"); 34} 35}

Use of collections in Polymorphism

Of course, the List <object> set can reference the ArrayList <object> set.

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.