Java upward transformation and downward Transformation (with a detailed example), java Transformation

Source: Internet
Author: User

Java upward transformation and downward Transformation (with a detailed example), java Transformation

Java upward and downward transformations (for example)

Examples of upward and downward Java transformation compiled by staying up late are very easy to understand ~~~~

1. Upward Transformation

Package com. sheepmu; class Animal {public void eat () {System. out. println ("parent class eating... ") ;}} class Bird extends Animal {@ Overridepublic void eat () {System. out. println ("subclass override the eatting of the parent class... ");} public void fly () {System. out. println ("subclass new method flying... ") ;}} public class Sys {public static void main (String [] args) {Animal B = new Bird (); // transforms B Up. eat (); // B. fly (); B points to the subclass object, but the subclass loses the fly () method sleep (new Male ( ); Sleep (new Female (); // The input parameter is a subclass -----!!} Public static void sleep (Human h) // the parameter of the method is the parent class ------!!! {H. sleep ();}}
Package com. sheepmu; public class Human {public void sleep () {System. out. println ("parent class human sleep .. ") ;}} class Male extends Human {@ Overridepublic void sleep () {System. out. println ("sleep man .. ") ;}} class Female extends Human {@ Overridepublic void sleep () {System. out. println ("Women sleep .. ");}}
Output:

The eatting of the parent class overwritten by the subclass...
Men sleep ..
Women sleep ..


Details:

1. Implementation of upward Transformation 

Animal B = new Bird (); // upward Transformation
B. eat (); // call the eat () method of the subclass.
B. fly (); // Error !!!!! ------- Although B points to a subclass object, at this time, the subclass is lost as the cost of upward TRANSFORMATION AND THE fly () method of the parent class ------
2. Why not directly Bird B = new Bird (); B. eat?

In this way, it does not reflect the abstract programming ideas of the Forward to the object, reducing the scalability of the Code.

3. What are the benefits of upward transformation?

Sleep (new Male (); // when calling a method, the input parameter is a subclass.
Sleep (new Female ());

Public static void sleep (Human h) // the parameter of the method is the parent class.

{

H. sleep ();

}

The above code is an upward transformation. If you do not need an upward transformation, you have to write several different sleeping methods here for each subclass ~~~~~~


Ii. Downward Transformation

Package com. sheepmu; class Fruit {public void myName () {System. out. println ("I'm a parent fruit... ") ;}} class Apple extends Fruit {@ Overridepublic void myName () {System. out. println ("I'm a subclass of apple... ");} public void myMore () {System. out. println ("I'm your little apple ~~~~~~ ") ;}} Public class Sys {public static void main (String [] args) {Fruit a = new Apple (); //. myName (); Apple aa = (Apple) a; // downward transformation, compilation and running will not go wrong (correct) aa. myName (); // The Sub-Class aa is called during the downward transformation. myMore (); Fruit f = new Fruit (); Apple aaa = (Apple) f. myName (); aaa. myMore ();}}
Output:

I am a subclass of apple...
I am a subclass of apple...
I'm your little apple ~~~~~~
Exception in thread "main"Java. lang. ClassCastException: Com. sheepmu. Fruit cannot be cast to com. sheepmu. Apple
At com. sheepmu. Sys. main (Sys. java: 30)


Details:

1. Correct downward Transformation

Fruit a = new Apple (); // upward Transformation
A. myName ();
Apple aa = (Apple) a; // downward transformation, compilation and running will not go wrong (correct)
Aa. myName ();
Aa. myMore ();

A pointsSubclass objectSo the sub-class instance aa can also point to ~~

After the downward transformation, because all objects point to subclass objects, all the methods called are subclass methods ~~

2. Unsafe downward Transformation

Fruit f = new Fruit ();
Apple aaa = (Apple) f; //-insecure --- downward transformation. No compilation error occurs but errors may occur during running.
Aaa. myName ();
Aaa. myMore ();

F is a parent class object. The aaa of the subclass instance must not point to the parent class f ~~~

3. Java introduces the concept of generics to Solve Unsafe downward transformation Problems

4. For the sake of secure type conversion, it is best to use if (A instanceof B) to determine the lower part of the sequence ~~


Java upward and downward Transformation

Which of the following statements?

If you need to use polymorphism, you should make an upward transformation. As for the reason for using polymorphism and when to use it, you only need to think about it by yourself, and you do not have to think about it for a long time, the amount of code is too large, and you will feel it slowly.

Downward transformation is not safe. Of course, if you have determined that he is a sub-class, you can go down. If you use polymorphism and use downward transformation, you can check whether your design is faulty.

Less inheritance and more implementation (not abuse)

If you use a downward transformation, what was the original purpose of the upward transformation? Consider this issue.

Like deserialization, we need to transform down to our own subclass, because deserialization produces an Object

Removing elements from a set also requires a downward transformation. You may not need to say that. This is something java has done for you. generics are implemented using Object erasure.

What is the difference between Java object-oriented multi-upward transformation and downward transformation?

Hello, here is an example to illustrate that it is easier to understand.
Here we will talk about object-oriented Transformation (basic data types are not object-oriented, so we will not talk about them here ).
The transformation in the object-oriented model only takes place in subclass and parent classes with inheritance relationships (the implementation of interfaces is also included here ).
Add a parent class: Man, Child class: man and woman.
Upward Transformation: Person p = new Man (); // forced type conversion is not required for upward Transformation
Downward Transformation: Man man = (Man) new Person (); // mandatory type conversion

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.