Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the upward and downward transformation of Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to analyze Java upward transformation and downward transformation, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

It's not hard to learn how to use upward and downward transitions, but why do you use it that way? I haven't figured it out many times. Do not understand the reason is usually learning to look at examples, and examples are generally relatively simple, there is no call between objects, it is generally an object to call its own method.

First of all, take a look at how to use the transformation.

In order to transform, we must first have inheritance. Inheritance is a mechanism of code reuse in object-oriented language. to put it simply, the subclass inherits the non-private properties and inheritable methods in the parent class, and then the subclass can continue to extend its own properties and methods.

Upward transformation: the subclass object becomes the parent class, and the parent class can be an interface. Formula: Father f = new Son (); Father is the parent class or interface, and son is the subclass.

Transition down: the parent object becomes a subclass. Formula: Son s = (Son) f

Example: upward transition

Package multistate;public class Human {public void sleep () {System.out.println ("Human sleep..");} public static void main (String [] args) {Human h = new Male (); / / upward transition h.sleep (); Male m = new Male (); / / Why transform m.sleep () upward; / / h.speak () This method cannot be compiled, error reporting that the Human class does not have this method}} class Male extends Human {@ Override public void sleep () {System.out.println ("Male sleep..");} public void speak () {System.out.println ("I am Male");}} class Female extends Human {@ Override public void sleep () {System.out.println ("Female sleep..");} public void speak () {System.out.println ("I am Female");}}

Note: upward transformation should not be forced. After the upward transformation, the reference of the parent class points to the property of the parent class. If the subclass overrides the method of the parent class, then the reference of the parent class points to or calls the method of the subclass, which is called dynamic binding. After the upward transformation, the parent class reference cannot call the subclass's own method, that is, the parent class does not have a subclass method, if the call cannot be compiled, such as the subclass's speak method.

Do I have to call the properties of the subclass? If you don't go down, you need to write getter methods for the properties you need.

Example:

Package multistate;public class Human {String name = "Human"; public String getName () {return this.name;} public void sleep () {System.out.println ("Human sleep..");} public static void main (String [] args) {Human h = new Male (); / / upward transition System.out.println (h.getName ());} class Male extends Human {String name = "Male"; public String getName () {return this.name @ Override public void sleep () {System.out.println ("Male sleep..");} public void speak () {System.out.println ("I am Male");}} class Female extends Human {String name = "Female"; public String getName () {return this.name;} @ Override public void sleep () {System.out.println ("Female sleep..");} public void speak () {System.out.println ("I am Female");}}

If you have to call a method that extends a subclass, such as the speak method, you can only transition down.

Example: transition Down

Security needs to be considered in the downward transition. If the object referenced by the parent class is the parent class itself, it is not safe during the downward transition, the compilation will not go wrong, but there will be java.lang.ClassCastException errors at run time. It can use instanceof to avoid errors such as whether it can be transformed downwards, and only objects that have gone through upward transformations can continue to transition downwards.

Package multistate;public class Human {public void sleep () {System.out.println ("Human sleep..");} public static void main (String [] args) {Human h = new Male (); / / upward transition Human H2 = new Human (); / / h.speak (); otherwise, the speak method cannot be called. Male m = (Male) h; m.speak (); / * * Male M1 = (Male) H2; m1.speak (); runtime errors occur at this time, so you can use instanceOF to judge * / if (H2 instanceof Male) {Male M1 = (Male) H2; m1.speak ();}} class Male extends Human {@ Override public void sleep () {System.out.println ("Male sleep..");} public void speak () {System.out.println ("I am Male") }}

After a long time, the upward transformation cannot have all the methods of the subclass, and it has to be transformed downwards. Wouldn't it be very convenient to directly Son s = new Son ()? I don't know if I'm the only one who started to learn about transformation.

Finally, I figured out that the reason was that my example was too simple to consider passing the object of the class to other functions.

Example: reflect the benefits of upward transformation and save code.

Package multistate;public class Human {public void sleep () {System.out.println ("Human sleep..");} public static void doSleep (Human h) {h.sleep ();} / / the argument passed at this time is the parent object, but passing the subclass object when it is actually called is an upward transformation. Public static void main (String [] args) {Human h = new Male (); / / upward transition doSleep (new Male ()); / / Anonymous subclass objects here. Of course, the above upward transformation formula should be used in practical application, and then the subclass objects should be passed in. This is good for the future transition down, and there is no downward transformation here, so anonymous class objects are directly used. DoSleep (new Female ());}} class Male extends Human {@ Override public void sleep () {System.out.println ("Male sleep..");}} class Female extends Human {@ Override public void sleep () {System.out.println ("Female sleep..");}}

If you do not move up, you must write two doSleep functions, one passing the Male class object and one passing the Female class object. These are still two subclasses. if there are many subclasses, you have to write a lot of the same functions, causing repetition.

Well, finally understand why the upward transformation, once the upward transformation, when the need to use the subclass method, the need to transition down, that is, why the downward transformation is also solved.

To sum up:

1. Assigning the subclass object directly to the parent class reference is called upcasting upward transformation, which does not require forced transformation.

For example, Father father = new Son ()

2. Assigning a parent class reference to a subclass object to a subclass reference is called downward transition (downcasting). In order to force a transition, if you want to make a downward transition, you must first make an upward transition. For security, you can use instanceof to judge.

For example, father is a parent class reference to a subclass object, and assigns father to the subclass reference son, that is, Son son = (Son) father.

The (Son) in front of father must be added to cast.

3. Upcasting will lose the methods unique to the subclass, but the methods of the subclass overriding parent class are valid, and the upward transformation can only reference the attributes of the parent class object. To reference the properties of the subclass object, write the getter function.

4, the role of upward transformation, reduce repetitive code, the parent class as parameters, call sometimes use subclasses as parameters, is to take advantage of the upward transformation. This makes the code concise. It embodies the abstract programming idea of JAVA.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you will support us more.

Title of this article: case analysis of upward and downward transformation of Java

The above is all about the upward and downward transformation of Java instance analysis, a comprehensive introduction of programming technology, operating system, database, web front-end technology and so on.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report