Reference casting and assignment:
It is always legal to assign a reference of a subclass type to a superclass type. For example, it is legal to write:
PrivilegedUser p;
User u;
p = new PrivilegedUser();
u = p; // legal without a cast
This is similar to promotion with primitive types, where a primitive variable is automatically converted to another primitive type. In this case, there is an automatic conversion from the PrivilegedUser reference type to the User reference type. This kind of automatic conversion only occurs from a subclass type to a superclass type. As we said earlier, a PrivilegedUser 'is-a' User (by inheritance) so this conversion makes sense.
Casting a subclass type to a superclass type
Reference types can be cast using the same mechanism as for primitive types: the desired type is put in parentheses in front of the expression whose type is to be changed.
For example, the assignment shown previously can be written using a cast as follows:
u = (User) p; // explicit cast to type User
This makes explicit that u will reference a different type of object. This cast is not required, however, because a PrivilegedUser
object is a User already, by inheritance. So, after this assignment, with or without a cast, the User reference u is referring to a PrivilegedUser object. This is safe because any method you invoke on a User can also be invoked on a PrivilegedUser. So in fact, a User reference could reference either a User or a PrivilegedUser kind of object. This is a general principle: a reference can refer to an object of its type, or to an object of any type below it in a class hierarchy, without the need for a cast.
Casting a superclass type to a subclass type
Another general principle is that it is not allowed for a reference to refer to a superclass type object. This means that it is not legal to write the following assignment statement:
p = u; // u could reference a User or PrivilegedUser object!
Why not? We cannot allow assignment of a User object reference to p because a User object has no password variable or setPassword method. If p references a User object, the setPassword method cannot be invoked on p. In general, we cannot allow this kind of assignment because we will no longer know what methods can be invoked on the referenced object. However, notice that the User reference u could in fact refer to a PrivilegedUser object. Therefore, if we have additional information and are convinced that the assignment will work, we can inform the system using the usual cast mechanism as follows:
p = (PrivilegedUser) u; // cast required for this assignment
The compiler will accept this, on the assumption that we have thought through the implications. Any problems with this assignment cannot be detected until the program is actually running. If u is referencing a PrivilegedUser object, this will not cause any problems at run time and we will be able to invoke all the PrivilegedUser methods on the object referenced by p. However, if our reasoning was incorrect, and u is referencing a User object, this assignment would cause your program to crash with what is known as a ClassCastException.
There is a mechanism for determining the type of object referenced by a variable, so that you can avoid crashing your program with this error. The facility we have described here may seem troublesome, but in fact it allows you to write ?exible code.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Reference casting and assignment homework and assignments? Live Reference casting and assignment experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Reference casting and assignment homework help, java assignment help and Reference casting and assignment projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours