Type conversions are often encountered in Java programming, and object type conversions mainly include 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 black or white, adult or child, that is to say, we are more inclined to use abstract concept "person". For example, sparrows are a type of bird (a subclass of birds), and birds are one of the animals (Children of animals). We often say this in reality: Sparrows are birds. These two statements are in fact the so-called upward transformation, popularly speaking is the sub-class transformation into a parent class. This also conforms to the Java-advocated abstract programming idea. Look at the following code:
Package a.b;
public class A {
public void A1 () {
System.out.println ("superclass");
}
}
Sub-Class B of a:
Package a.b;
public class B extends A {
public void A1 () {
System.out.println ("Childrenclass"); Overriding the parent class method
}
The public void B1 () {}//b class defines its own new method
}
Class C:
Package a.b;
public class C {
public static void Main (string[] args) {
A = new B (); Upward transformation
A.A1 ();
}
}
If C is running, is the output superclass or Childrenclass? Not the superclass you originally expected, but the childrenclass. This is because a actually points to a subclass object. Of course, you don't have to worry, the Java virtual opportunity automatically and accurately identifies which specific method to call. However, because of the upward transformation, the A object loses a different method from the parent class, such as B1 (). One might ask: Is this not superfluous? We can write it this way:
b A = new B ();
A.A1 ();
That's true! In this way, it loses its abstraction-oriented programming features and reduces scalability. In fact, not only this, the upward transformation can also reduce the programming workload. Look at the following Monitor class monitor:
Package a.b;
public class monitor{
public void DisplayText () {}
public void Displaygraphics () {}
}
Liquid crystal Display class Lcdmonitor 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");
}
}
Cathode ray Tube Display class Crtmonitor is naturally also 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");
}
}
Plasma display Plasmamonitor is also a sub-category 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 code above has a lot of duplicate code and is not easy to maintain. With an upward transformation, the code can be more concise:
Package a.b;
public class Mymonitor {
public static void Main (string[] args) {
Run (new Lcdmonitor ()); Upward transformation
Run (new Crtmonitor ()); Upward transformation
Run (new Plasmamonitor ()); Upward transformation
}
public static void Run (monitor monitor) {//parent class instance as parameter
Monitor.displaytext ();
Monitor.displaygraphics ();
}
}
We can also use interfaces, such as:
Package a.b;
Public interface Monitor {
abstract void DisplayText ();
abstract void Displaygraphics ();
}
Change the liquid crystal display class Lcdmonitor slightly:
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");
}
}
Crtmonitor, Plasmamonitor class modification method and Lcdmonitor similar, and mymonitor can not make no changes.
As can be seen, the upward transformation embodies the polymorphism of the class and enhances the simplicity of the program.
5.13.2 Downward transformation
The transformation of a subclass into a parent class is an upward transformation, and conversely, the transformation of a parent class into a subclass is a downward transformation. However, a downward transition may pose some problems: we can say that sparrows are birds, but we cannot say that birds are sparrows. Take a look at the following example:
Class A:
Package a.b;
public class A {
void Amthod () {
System.out.println ("A method");
}
}
Sub-Class B of a:
Package a.b;
public class B extends A {
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 parent class Amthod (), A1 lost Class B Method BMethod1 (), BMETHOD2 ()
b B1 = (b) A1; Transition down, compile error-free, run-time error-free
B1.amthod (); Call the parent Class A method
B1.bmethod1 (); Call Class B method
B1.BMETHOD2 (); Call Class B method
A a2 = new A ();
b b2 = (b) A2; Transition down, compile error-free, run-time error
B2.amthod ();
B2.bmethod1 ();
B2.BMETHOD2 ();
}
}
From the code above, we can conclude that a downward transition requires the use of casts. Run the C program and the console will output:
Exception in thread "main" JAVA.LANG.CLASSCASTEXCEPTION:A.B.A cannot is cast to a.b.b at
A.b.c.main (C.JAVA:14)
A method
A method
B Method 1
B Method 2
In fact, the comment after the downward transformation code of the Blackbody section has prompted you to have a run-time error. Why did the previous sentence go down the code, and then the code went wrong? This is because A1 points to an object of subclass B, so instance object B1 of subclass B can also point to A1. While A2 is a parent class object, the Subclass object B2 cannot point to the parent class object A2. So how do you avoid a run-time classcastexception exception when performing a downward transition? Use the 5.7.7 section to learn the instanceof on it. Let's change the code for Class C:
A a2 = new A ();
if (A2 instanceof B) {
b b2 = (b) A2;
B2.amthod ();
B2.bmethod1 ();
B2.BMETHOD2 ();
}
When this is done, you do not have to worry about classcastexception exceptions when you type conversions.
Java transformation (up and down transformation)