java-upward transformation and downward transformation

Source: Internet
Author: User
Tags class manager

Let's review the Java basics: In Java, the value of any object variable is a reference to an object stored in another place. An object variable does not actually contain an object, but simply refers to an object.

A = new A ();

The expression new A () constructs an object of type A, whose return value is also a reference and a reference to the newly created object. We store this reference in variable a of type A.


Upward transformation: Assigns a reference to a subclass to a superclass variable. This is safe and does not have to be forced type conversions.

For example: Father f = new Son ();

Cons: Leads to loss of some methods and properties. F can now call only the methods and properties defined in the Father class, and the methods and properties in the son class are far behind.


Downward transformation: Assigns a reference to a superclass to a subclass variable, which must be type-cast.

For example:

Father f = new Son (); son s = (son) F;

A downward transition is unsafe, as there may be situations where a "false" object is present, such as

Father f = new Father (); son s = (son) F;
Running the above program will throw: Java.lang.ClassCastException exception.


Here is a small example for reference:

public class Person{public static void Main (string[] args) {Manager M1 = new manager ("Zhu", 8000,2000); Employee e1 = new Manager ("Li", 8000,1000); Employee e2= New Employee ("Zhang", 8000); Employee e = (manager) m1;//up transition Manager m = (manager) e1;//down, this is correct, the runtime also does not error M = (manager) e2;//downward transformation, which is incorrect, belongs to the False object, compile can be too,    But run times wrong}}/** * Employee class * @author RTTD * */class employee{private String name;    private double salary;    Public Employee (String aName, double asalary) {name = AName;    salary = Asalary;    Public Employee () {This ("", 0);    } public void Raisesalary (double percent) {double raise = this.salary*percent/100;    This.salary + = raise; }public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public double getsalary () {return salary;} public void getsalary (int a) {System.out.println ("I am the employee class, is the parent class of the manager class Oh");} public void Setsalary (double salary) {this.salary = salary;}} /** * Manager class, sub-class belonging to employee class * @author RTTD * */class Manager extends employee{private double bonus;p ublic Manager (String aname,double asalary,double abonus) {super (aName, asalary); this.bonus = Abonus;} Public Manager () {This ("", 0,0);} Public double getsalary () {Double base = Super.getsalary (); return base + bonus;} public void getsalary (int a) {System.out.println ("I am the manager class, is a subclass of the employee Class");}}

Because Java has polymorphic properties, most of the time, we can use the dynamic binding mechanism to automatically find the corresponding method when invoking the method of an object with an inheritance relationship. When it is necessary to invoke certain methods in some subclasses with coercion type conversion, be careful about security. You can look at this post for the polymorphic nature of Java.


java-upward transformation and downward transformation

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.