Summary of the upward transformation of Java

Source: Internet
Author: User

The "Think of Java" has the following description of ' upward transformation '

After reading the very circle, so I found some description on the internet, and made a simple summary of their own

A simple example

Class a{
public void A1 () {
System.out.println ("Parent class Method 1");
}
public void A2 () {
System.out.println ("Parent class Method 2");
}
}

Class B extends a{
public void A1 () {
System.out.println ("Subclass Method 1");
}
public void B2 () {
System.out.println ("Subclass Method 2");
}
}

public class c{
public static void Main (String args[]) {
A a=new B ();//upcast

a.a1;

A.A2;
}
}

Output: System.out.println ("Subclass Method 1");

System.out.println ("Parent class Method 2");

Description

A a=new B ();
To move up, the popular point is to create a B subclass object and then force it to transform to a Class A object. So the Class A A1 () method covers the A1 () method in quilt Class B, and B2 () in the subclass is lost.

From CSDN. Easy to understand.
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.

Summary of the upward transformation of Java

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.