Some embodiment of polymorphism in Java, rewriting and wrapper classes

Source: Internet
Author: User

1. Heterogeneous arrays

Polymorphism of the expression and double can directly convert int a=3;double to accept int b=a;
Animal dog=new Dog ();//Instantiate a Dog object modification before notice upward conversion
Down conversion//type Restore
instanceof determines the type of the attribute, which can be used to avoid type conversion exceptions.
D instanceof Dog, judging if D is not a dog type, if it is complete forced conversion of dog d= (dog) dog;

Different subclasses belonging to the same parent class:

Animal is the parent class of dog Cut

Animal dog=new Dog ();//Use a reference to the parent class to point to the instantiation of the child class.
Cut cut =new cut ();
Animal [] animal=new animal[2];
Animal[0]=dog;
Animal[1]=cut;

2. Rewrite

Overriding note that during the rewrite process, the access modifier can be enlarged, and the return value can be reduced;


Parent class Method:
Public Animal Lingyang (Animal d) {
return D;
}
Subclass Overrides:

Public Dog Lingyang (Animal d) {
Dog dong = (dog) D;
return dog;
}

3.8 Basic data types corresponding to the wrapper class
int-->integer;

double-->double;

float-->float;

byte-->byte;

boolean-->boolean;

short-->short;

long-->long;

char-->character;


int b=1;
Integer c=b;---------->//the process of converting a basic data type to a wrapper class is called boxing

Integer b=1;
int c=b;-----> unpacking;

The above are all small knowledge, * * *

Some embodiment of polymorphism in Java, rewriting and wrapper classes

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.