In Java programming, type conversion is often encountered. Object type conversion mainly includes upward transformation and downward transformation.
5.13.1 upward Transformation
We often say this in reality: this person can sing. Here, we do not care whether this person is a black person or a white person, an adult or a child. That is to say, we prefer to use the abstract concept "person ". For example, a sparrow is a type of bird (a subclass of birds), and a bird is a type of animal (a subclass of animals ). We often say this in reality: Sparrow is a bird. These two statements are actually called "upward transformation". In other words, sub-classes are transformed into parent classes. This is also in line with the abstract programming philosophy advocated by Java. Let's look at the following code:
Package A. B;
Public Class {
Public void A1 (){
System. Out. println ("superclass ");
}
}
Subclass B of:
Package A. B;
Public Class B extends {
Public void A1 (){
System. Out. println ("childrenclass"); // overwrite the parent class Method
}
Public void B1 () {}// Class B defines its own New Method
}
Class C:
Package A. B;
Public class c {
Public static void main (string [] ARGs ){
A A = new B (); // upward Transformation
A. A1 ();
}
}
If C is run, is the output superclass or childrenclass? It is not the superclass you expected, but childrenclass. This is because a actually points to a subclass object. Of course, you don't have to worry about it. the Java Virtual Machine automatically and accurately identifies the specific method to call. However, due to the upward transformation, the object will lose methods different from the parent class, such as B1 (). Some may ask: Isn't that an option? We can write like this:
B A = new B ();
A. A1 ();
Indeed! However, the abstract programming features are lost and the scalability is reduced. In fact, more than that, the upward transformation can also reduce the programming workload. Let's look at the following monitor:
Package A. B;
Public class monitor {
Public void displaytext (){}
Public void displaygraphics (){}
}
LCD monitor is a subclass of monitor:
Package A. B;
Public class lcdmonitor extends monitor {
Public void displaytext (){
System. Out. println ("LCD display text ");
}
Public void displaygraphics (){
System. Out. println ("LCD display graphics ");
}
}
The crtmonitor type of the cathode ray tube display is naturally a subclass of monitor:
Package A. B;
Public class crtmonitor extends monitor {
Public void displaytext (){
System. Out. println ("CRT display text ");
}
Public void displaygraphics (){
System. Out. println ("CRT display graphics ");
}
}
Plasmamonitor is also a subclass of monitor:
Package A. B;
Public class plasmamonitor extends monitor {
Public void displaytext (){
System. Out. println ("plasma display text ");
}
Public void displaygraphics (){
System. Out. println ("plasma display graphics ");
}
}
Now there is a mymonitor class. Assuming there is no upward transformation, the mymonitor class code is as follows:
Package A. B;
Public class mymonitor {
Public static void main (string [] ARGs ){
Run (New lcdmonitor ());
Run (New crtmonitor ());
Run (New plasmamonitor ());
}
Public static void run (lcdmonitor Monitor ){
Monitor. displaytext ();
Monitor. displaygraphics ();
}
Public static void run (crtmonitor Monitor ){
Monitor. displaytext ();
Monitor. displaygraphics ();
}
Public static void run (plasmamonitor Monitor ){
Monitor. displaytext ();
Monitor. displaygraphics ();
}
}
You may have realized that the above Code has a lot of repeated code and is not easy to maintain. With the upward transformation, the code can be simpler:
Package A. B;
Public class mymonitor {
Public static void main (string [] ARGs ){
Run (New lcdmonitor (); // upward Transformation
Run (New crtmonitor (); // upward transition
Run (New plasmamonitor (); // upward Transformation
}
Public static void run (monitor) {// The parent class instance is used as the parameter
Monitor. displaytext ();
Monitor. displaygraphics ();
}
}
We can also use the interface method, for example:
Package A. B;
Public interface monitor {
Abstract void displaytext ();
Abstract void displaygraphics ();
}
Slightly modify the LCD monitor:
Package A. B;
Public class lcdmonitor implements monitor {
Public void displaytext (){
System. Out. println ("LCD display text ");
}
Public void displaygraphics (){
System. Out. println ("LCD display graphics ");
}
}
The methods for modifying crtmonitor and plasmamonitor classes are similar to those of lcdmonitor, while mymonitor can be modified without any modification.
We can see that the upward transformation reflects the class polymorphism and enhances the simplicity of the program.
5.13.2 downward Transformation
The transformation from a subclass to a parent class is an upward transformation. In other words, the transformation from a parent class to a subclass is a downward transformation. However, the downward transformation may bring about some problems: we can say that the sparrow is a bird, but it cannot be said that the bird is a sparrow. Let's look at the following example:
Class:
Package A. B;
Public Class {
Void amthod (){
System. Out. println ("A method ");
}
}
Subclass B of:
Package A. B;
Public Class B extends {
Void bmethod1 (){
System. Out. println ("B method 1 ");
}
Void bmethod2 (){
System. Out. println ("B method 2 ");
}
}
Class C:
Package A. B;
Public class c {
Public static void main (string [] ARGs ){
A a1 = new B (); // upward Transformation
A1.amthod (); // call the parent class amthod (), A1 loss of Class B Methods bmethod1 (), bmethod2 ()
B b1 = (B) A1; // downward transformation, no compilation errors, no running errors
B1.amthod (); // call the parent class A Method
B1.bmethod1 (); // call class B Methods
B1.bmethod2 (); // call class B Methods
A a2 = new ();
B b2 = (B) A2; // downward transformation, no compilation error, and errors will occur during runtime
B2.amthod ();
B2.bmethod1 ();
B2.bmethod2 ();
}
}
From the above code, we can draw a conclusion:Forced conversion is required for downward transformation.Run the C program and the console will output:
Exception in thread "Main" Java. Lang. classcastexception: A. B. A cannot be cast to A. B. B
A. B. C. Main (C. Java: 14)
A Method
A Method
B method 1
B method 2
In fact, the comments after the downward transformation code of the simhei section have prompted you to encounter a runtime error. Why can the previous sentence Transform code down, but the other sentence code fails? This is because A1 points to an object of subclass B, so Instance Object B1 of subclass B can also point to A1. A2 is a parent class object, and B2 of the subclass object cannot point to A2 of the parent class object. So how to avoid the exception of classcastexception during the execution of the downward transformation? You can use the instanceof that has been learned in section 5.7.7. Let's modify the C class code:
A a2 = new ();
If (A2 instanceof B ){
B b2 = (B) A2;
B2.amthod ();
B2.bmethod1 ();
B2.bmethod2 ();
}
After such processing, you don't have to worry about classcastexception exceptions during type conversion.
In Java programming, type conversion is often encountered. Object type conversion mainly includes upward transformation and downward transformation.
5.13.1 upward Transformation
We often say this in reality: this person can sing. Here, we do not care whether this person is a black person or a white person, an adult or a child. That is to say, we prefer to use the abstract concept "person ". For example, a sparrow is a type of bird (a subclass of birds), and a bird is a type of animal (a subclass of animals ). We often say this in reality: Sparrow is a bird. These two statements are actually called "upward transformation". In other words, sub-classes are transformed into parent classes. This is also in line with the abstract programming philosophy advocated by Java. Let's look at the following code:
Package A. B;
Public Class {
Public void A1 (){
System. Out. println ("superclass ");
}
}
Subclass B of:
Package A. B;
Public Class B extends {
Public void A1 (){
System. Out. println ("childrenclass"); // overwrite the parent class Method
}
Public void B1 () {}// Class B defines its own New Method
}
Class C:
Package A. B;
Public class c {
Public static void main (string [] ARGs ){
A A = new B (); // upward Transformation
A. A1 ();
}
}
If C is run, is the output superclass or childrenclass? It is not the superclass you expected, but childrenclass. This is because a actually points to a subclass object. Of course, you don't have to worry about it. the Java Virtual Machine automatically and accurately identifies the specific method to call. However, due to the upward transformation, the object will lose methods different from the parent class, such as B1 (). Some may ask: Isn't that an option? We can write like this:
B A = new B ();
A. A1 ();
Indeed! However, the abstract programming features are lost and the scalability is reduced. In fact, more than that, the upward transformation can also reduce the programming workload. Let's look at the following monitor:
Package A. B;
Public class monitor {
Public void displaytext (){}
Public void displaygraphics (){}
}
LCD monitor is a subclass of monitor:
Package A. B;
Public class lcdmonitor extends monitor {
Public void displaytext (){
System. Out. println ("LCD display text ");
}
Public void displaygraphics (){
System. Out. println ("LCD display graphics ");
}
}
The crtmonitor type of the cathode ray tube display is naturally a subclass of monitor:
Package A. B;
Public class crtmonitor extends monitor {
Public void displaytext (){
System. Out. println ("CRT display text ");
}
Public void displaygraphics (){
System. Out. println ("CRT display graphics ");
}
}
Plasmamonitor is also a subclass of monitor:
Package A. B;
Public class plasmamonitor extends monitor {
Public void displaytext (){
System. Out. println ("plasma display text ");
}
Public void displaygraphics (){
System. Out. println ("plasma display graphics ");
}
}
Now there is a mymonitor class. Assuming there is no upward transformation, the mymonitor class code is as follows:
Package A. B;
Public class mymonitor {
Public static void main (string [] ARGs ){
Run (New lcdmonitor ());
Run (New crtmonitor ());
Run (New plasmamonitor ());
}
Public static void run (lcdmonitor Monitor ){
Monitor. displaytext ();
Monitor. displaygraphics ();
}
Public static void run (crtmonitor Monitor ){
Monitor. displaytext ();
Monitor. displaygraphics ();
}
Public static void run (plasmamonitor Monitor ){
Monitor. displaytext ();
Monitor. displaygraphics ();
}
}
You may have realized that the above Code has a lot of repeated code and is not easy to maintain. With the upward transformation, the code can be simpler:
Package A. B;
Public class mymonitor {
Public static void main (string [] ARGs ){
Run (New lcdmonitor (); // upward Transformation
Run (New crtmonitor (); // upward transition
Run (New plasmamonitor (); // upward Transformation
}
Public static void run (monitor) {// The parent class instance is used as the parameter
Monitor. displaytext ();
Monitor. displaygraphics ();
}
}
We can also use the interface method, for example:
Package A. B;
Public interface monitor {
Abstract void displaytext ();
Abstract void displaygraphics ();
}
Slightly modify the LCD monitor:
Package A. B;
Public class lcdmonitor implements monitor {
Public void displaytext (){
System. Out. println ("LCD display text ");
}
Public void displaygraphics (){
System. Out. println ("LCD display graphics ");
}
}
The methods for modifying crtmonitor and plasmamonitor classes are similar to those of lcdmonitor, while mymonitor can be modified without any modification.
We can see that the upward transformation reflects the class polymorphism and enhances the simplicity of the program.
5.13.2 downward Transformation
The transformation from a subclass to a parent class is an upward transformation. In other words, the transformation from a parent class to a subclass is a downward transformation. However, the downward transformation may bring about some problems: we can say that the sparrow is a bird, but it cannot be said that the bird is a sparrow. Let's look at the following example:
Class:
Package A. B;
Public Class {
Void amthod (){
System. Out. println ("A method ");
}
}
Subclass B of:
Package A. B;
Public Class B extends {
Void bmethod1 (){
System. Out. println ("B method 1 ");
}
Void bmethod2 (){
System. Out. println ("B method 2 ");
}
}
Class C:
Package A. B;
Public class c {
Public static void main (string [] ARGs ){
A a1 = new B (); // upward Transformation
A1.amthod (); // call the parent class amthod (), A1 loss of Class B Methods bmethod1 (), bmethod2 ()
B b1 = (B) A1; // downward transformation, no compilation errors, no running errors
B1.amthod (); // call the parent class A Method
B1.bmethod1 (); // call class B Methods
B1.bmethod2 (); // call class B Methods
A a2 = new ();
B b2 = (B) A2; // downward transformation, no compilation error, and errors will occur during runtime
B2.amthod ();
B2.bmethod1 ();
B2.bmethod2 ();
}
}
From the above code, we can draw a conclusion:Forced conversion is required for downward transformation.Run the C program and the console will output:
Exception in thread "Main" Java. Lang. classcastexception: A. B. A cannot be cast to A. B. B
A. B. C. Main (C. Java: 14)
A Method
A Method
B method 1
B method 2
In fact, the comments after the downward transformation code of the simhei section have prompted you to encounter a runtime error. Why can the previous sentence Transform code down, but the other sentence code fails? This is because A1 points to an object of subclass B, so Instance Object B1 of subclass B can also point to A1. A2 is a parent class object, and B2 of the subclass object cannot point to A2 of the parent class object. So how to avoid the exception of classcastexception during the execution of the downward transformation? You can use the instanceof that has been learned in section 5.7.7. Let's modify the C class code:
A a2 = new ();
If (A2 instanceof B ){
B b2 = (B) A2;
B2.amthod ();
B2.bmethod1 ();
B2.bmethod2 ();
}
After such processing, you don't have to worry about classcastexception exceptions during type conversion.