Java class inheritance summarizes conversion problems between a parent class type and a subclass type (GO)

Source: Internet
Author: User
Tags class manager

Java class Inheritance summarizes conversion problems between a parent class type and a subclass type

This article will describe the conversion problem between the parent class type and the subclass type through an instance, and this very special problem often leads to some potential dangers. For a whole night, you're debugging a program to solve a maddening java.lang.ArrayStoreException exception.

1. A reference to a subclass array can be replaced with a reference to an array of super-classes

2. Child class reference children can be converted to the parent class reference parent (this assumes that the parent is a parental object, and child is a subclass object), but it is not possible to invoke the specific method of children through the parent

Class employee{
protected String name;
protected long salary;
Public Employee (String aName) {
THIS.name = AName;
}

public void Setsalary (long asalary) {
This.salary = asalary;
}

Public long getsalary () {
return this.salary;
}
}

Class Manager extends employee{
protected long Bonus;
Public Manager (String aName) {
Super (AName);
}

public void Setbonus (long Abonus) {
This.bonus = Abonus;
}

Public long getsalary () {
Return super.getsalary () +this.bonus;
}
}

public static void Main (string[] args) {
Note that managers and employees refer to the same array
manager[] managers = new MANAGER[10];
employee[] employees = managers;
(a) Note that the following three statements can be passed, but the runtime throws an exception
Employees[0] = new Employee ("Abel"); Java.lang.ArrayStoreException exception thrown here at runtime
Managers[0].setsalary (1000);
Managers[0].setbonus (500);
(b) This statement compiles and runs without problems
Employees[0] = new Manager ("Abel");
Employees[0].setsalary (1000);
Managers[0].setsalary (1000);
Managers[0].setbonus (500);
}

Summary:
1. In Java, an object variable is polymorphic, an employee can reference an instance of an employee type, or an instance of a subclass manager type, but a manager cannot reference an instance of an employee type, for example:
   (1) Employee e = new Manager ("Abel") is legal
   (2) manager   m = new Employee ("Abel") It's not legal.
  reason is easy to understand, manager and employee is IS-A relationship, any manager is a emplpyee, as shown in (1), reference e can be references an instance of the manager type , which means that you can background-color: #ffcc99; any inheritance in >manager (Note that E cannot use the unique method of the manager, such as E.setbonus (1000) is not legal), The subtext of the employee E = new Manager ("Abel") means that E describes Manager,e as an employee from the employee's perspective, and whatever the E refers to, the compiler will look at it as an employee.

2. It is understandable that the code above (a) cannot be run, although Employees[0] is an employee type at the time of declaration, but employee[] employees = managers; specified employees[i] The point must be an instance of the manager type, so employees[0] = new Employee ("Abel"); This statement will inevitably throw an exception.
(a) the code employees[0] = new Employee ("Abel"), which can be justified by compiling, because the type of employees declaration is employee[], then employees[0] You can naturally accept a reference to an employee type, but actually

employee[] Employees = managers; The process of assigning the value of employees must be the manager type, from the above example can be seen, run time will do such checks, but compile time will not be checked so deep, Only check the declaration type of the variable, it seems the Java compiler is not smart enough AH:)

3. In the actual programming process, the use of the object variable polymorphism must be noted (especially when using the array), or compile through, but run an error, this mistake is likely to let you debug a night to find the problem!!

Java class inheritance summarizes conversion problems between a parent class type and a subclass type (GO)

Related Article

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.