A graph explaining---java polymorphism

Source: Internet
Author: User

1. Up transformation: The compiler does it automatically and does not need to be declared

New= s;   (equivalent to an internal object instance pointing to snowboard, all classes inherit from the object class)

① when O tries to refer to Snowboard's unique method, it will not succeed.
② when o references a quilt class override method, the method of the subclass is called

2. Downward transformation: Forced type conversion, need to declare

① point to the inside, you can change the direction of the outside at any time
Object o = new Snowboard ();
Snowboard s = (Snowboard) o;

② the parent class reference must now be directed to a subclass object, otherwise the downward transition is unsuccessful
Object o = new Object ();
Snowboard s = (Snowboard) o; This downward transformation is unsuccessful because the O reference is an instance of the object class, and there is no quilt class inheritance.

3. Three uses of polymorphism:

 1. The reference type can be the parent of the actual object type
Animal [] animals = new Animal [5];
animals [0] = new Dog ();
animals [1] = new Cat ();
animals [2] = new Wolf ();
animals [3] = new Hippo ();
animals [4] = new Lion ();
2. Parameters can be polymorphic
class Ver {
Public void Giveshot (Animal a) {
a.makenoise ();
}
}

class Petowner {
Public voi Start () {
Vet v = new Vet ();
Dog d = new Dog ();
Hippo h = new Hippo ();
V.giveshot (d);      
v.giveshot (h);     
}
}

3. Return value polymorphism: The first line of code P375

Public class MyService extends Service {
private Downloadbinder Mbinder = new Downloadbinder ();       
class Downloadbinder extends Binder {
Public void Startdownload () {
log.d ("MyService", "Startdownload executed");
}
public int getprogress () {
log.d ("MyService", "getprogress executed");
return 0;
}
}
@Override//When the activity and service are successfully bound, this method is recalled
public ibinder onbind (Intent Intent) {
       return mbinder; //Binder extends Object implements IBinder, inheritance relationship: IBinder > Binder > Downloadbinder
}
}

===================================================================================

private Serviceconnection connection = new Serviceconnection () {
@Override
Public void onservicedisconnected (componentname name) {
}
@Override
Public void onserviceconnected (componentname name, IBinder service) { //service returned Mbinder (actually pointing to IBinder)
Downloadbinder = (myservice.downloadbinder) service; //So turn downward into downloadbinder.
downloadbinder.startdownload ();
downloadbinder.getprogress ();
}
};



4. References:

①http://www.cnblogs.com/mengdd/archive/2012/12/25/2832288.html②headfirst Java

A graph explaining---java polymorphism

Related Article

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.