Java know how much (33) type conversions for polymorphic objects

Source: Internet
Author: User

The object type conversion referred to here refers to an object that has an inheritance relationship, not an object of any kind. The Java runtime throws a Java.lang.ClassCastException exception when coercion casts on objects that do not have an inheritance relationship.

In the inheritance chain, we convert the subclass to the parent class as "up transformation" and the parent class to the subclass transformation called "down transformation".

Many times, we define a variable as the type of the parent class, but refer to the object of the subclass, which is the process of upward transformation. When the program runs, it implements the invocation of a subclass method by dynamic binding, that is, polymorphism.

Sometimes, however, in order to accomplish something that the parent class does not have, we need to turn the object of the upward transformation into a subclass, calling the method of the subclass, which is the downward transformation.

Note: You cannot cast an object of the parent class directly to a subclass type, but you can only convert the sub-class object that is being transformed to a subclass type again. In other words, the subclass object must be transformed upward before it can be transformed downward. Take a look at the following code:
1  Public classDemo {2      Public Static voidMain (String args[]) {3Superclass Superobj =Newsuperclass ();4Sonclass sonobj =NewSonclass ();5         //The following code runs with an exception and cannot convert the parent object directly to a subclass type6         //Sonclass sonObj2 = (sonclass) superobj;7         //first up, then down.8Superobj =sonobj;9Sonclass sonObj1 =(Sonclass) superobj;Ten     } One } A classsuperclass{} - classSonclassextendssuperclass{}

Remove the comment from line 7th, and the runtime throws an exception, but the compilation can pass.


Because there is a risk of a downward transition, be sure to use the instanceof operator to determine if the object is the subclass you want when you receive a reference to the parent class, see the following code:
1  Public classDemo {2      Public Static voidMain (String args[]) {3Superclass Superobj =Newsuperclass ();4Sonclass sonobj =NewSonclass ();5         //Superobj is not an instance of the Sonclass class6         if(SuperobjinstanceofSonclass) {7Sonclass sonObj1 =(Sonclass) superobj;8}Else{9System.out.println ("① cannot be converted");Ten         } OneSuperobj =sonobj; A         //Superobj is an instance of the Sonclass class -         if(SuperobjinstanceofSonclass) { -Sonclass SonObj2 =(Sonclass) superobj; the}Else{ -System.out.println ("② cannot be converted"); -         } -     } + } - classsuperclass{} + classSonclassextendssuperclass{}

Operation Result:

① cannot be converted

Summary: Object type conversions are checked at program run time, the upward transformation is automatic, and the object that is transitioning down must be a subclass of the current reference type.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java know how much (8) class library and its organizational structure

Java know how much (9) Import and Java class search path

Java know how much (10) data types and variables

Java know how much (11) data type conversions

Java know how many (12) operators

Java know how much (13) Process Control

Java know how many (14) arrays

Java know how much (15) string

Java know how much (StringBuffer) and Stringbuider

Java know how much (17) emphasize the programming style

Java know how many (18) classes are defined and instantiated

Java know how many (19) access modifiers (access control characters)

Java knows how many (20) variables are scoped

Java know how much (+) This keyword is detailed

Java know how many (22) method overloads

Java know how much (23) the basic run order of classes

Java know how much (24) packaging class, unpacking and packing detailed

Java know how much (25) More about Java package

Java know how much (26) claim rules for source files

Java know how much (27) the concept and implementation of inheritance

Java know how many super keywords

Java know how much (29) Overwrite and reload

Java know how much (30) polymorphic and dynamic binding

Java know how many static keywords and Java static variables and static methods

Java know how much (instanceof)

Java know how much (33) type conversions for polymorphic objects

Java know how much (33) type conversions for polymorphic objects

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.