Object-oriented (advanced) -- Object Polymorphism
Master the use of upward transformation of objects for downward Transformation
Understanding the limitations of object Transformation
Object polymorphism is an important property of object orientation.
The orientation mainly includes the following two manifestations:
Method overloading and overwriting
Object Polymorphism
There are two types of object polymorphism:
Upward transition: subclass Object> parent class Object
The upward transformation process is automatically completed. format:
Object upward transition: parent class Object = subclass instance
Downward transition: parent class Object> subclass object
The type of the subclass to be converted must be specified in the following format:
Object downward transition: subclass object = (subclass) parent class instance
Example of object upward Transformation:
Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo01 {
Public static void main (string ARGs []) {
B = new B (); // defines subclass instantiation
A A = B; // The relationship between the above-mentioned Transformations
A. fun1 (); // This method has been overwritten by the quilt class
}
}
Example of object downward Transformation
Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo02 {
Public static void main (string ARGs []) {
A A = new B (); // upward Transformation
B = (B) A; // downward Transformation
B. fun1 ();
B. fun2 ();
B. fun3 ();
}
}
Note: Requirements for object downward Transformation
Before you perform the downward transformation of an object, you must first perform the upward transformation of the object. Otherwise, an object conversion exception occurs.
Incorrect transformation example:
Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo03 {
Public static void main (string ARGs []) {
A A = new A (); // upward Transformation
B = (B) A; // downward Transformation
B. fun1 ();
B. fun2 ();
B. fun3 ();
}
}
Error code:
Exception in thread "Main" Java. Lang. classcastexception: A cannot be cast to B
At poldemo03.main (poldemo03.java: 20)
The role of object polymorphism:
The following requirements exist:
Design a method that can interface any subclass object of Class A and call the method.
Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("B --> Public void fun3 (){}");
}
};
Class C extends {
Public void fun1 (){
System. Out. println ("c --> Public void fun1 (){}");
}
Public void fun4 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo04 {
Public static void main (string ARGs []) {
Fun (New B ());
Fun (New C ());
}
Public static void fun (){
A. fun1 ();
}
}