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