Java up-conversion (upcasting) and down-conversion (downcasting) Distinction Examples

Source: Internet
Author: User

This afternoon in the practice of instanceof usage of the Java upcating and downcasting seems to be associated with this, so he wrote a related case, in this with the friends see the pro share, hope to help the partners are learning.


Package com.instanceoftest;



/** Java up and down transformation, and instanceof method to prevent down-conversion errors
* Interface Description
In the implementation of the *interface method, the demo can only have static data members that cannot be modified (that is, it must be static final, but the data members are not generally defined in interface).
* All the member methods are abstract. In a sense, interface is a special form of abstract class.
*
* @author MENGFL
*
*/
Interface animal{
public static final String color = "Blue";
abstract void voice ();
abstract void eat ();

}
Class Flyanimal implements animal{


public void Eat () {
TODO auto-generated Method Stub
System.out.println ("Parent class Flyanimal eat worm!");


}


public void Voice () {
TODO auto-generated Method Stub
System.out.println ("Parent class Flyanimal sing wonderful!");
}
}
/*
* Inherit interface
*/
Class Parrot extends Flyanimal {
@Override
public void Eat () {
TODO auto-generated Method Stub
System.out.println ("Parrot eat worm!");
}
@Override
public void Voice () {
TODO auto-generated Method Stub
System.out.println ("Parrot Sing wonderful!");
}
public void jump () {
System.out.println ("Parrot can jump!");

}
}


Class Instanceoftest {
public static void Main (string[] args) {
Animal A1 = new Parrot ();
A1.eat ();//upward transformation, can only call the Eat () method in the parent class animal
Flyanimal A2 = new Parrot ();//Upward transformation
A2.eat ();
A2.voice ();//upward transformation, only the Eat () and voice () methods in the parent class Flyanimal can be called.


// In the downward transformation process, there are two situations:
//
// Case one: If the object referenced by the parent class refers to a sub-class object that is pointed to, it is safe in the process of downward transformation. That is, compiling does not make mistakes.
//
// Scenario Two: If the parent class refers to an object that is the parent class itself, it is unsafe in the process of downward transformation and compiles without errors, but java.lang.ClassCastException errors occur at run time. It can use instanceof to avoid errors like this. Examples are as follows:
Parrot a3 = (Parrot) a2;//security down transformation, compile and run without error. Note that this is based on an upward transformation.
A3.eat ();
A3.voice ();
A3.jump ();
Flyanimal a4 = new Flyanimal ();
When you run to the following Parrot A5 = (Parrot) A4 method, the following exception is reported
Exception in thread "main" Java.lang.ClassCastException:com.instanceoftest.FlyAnimal cannot is cast to Com.instanceoftest.Parrot

// Parrot A5 = (Parrot) A4; Unsafe down-conversion, compile error-free but run errors
// A5.eat ();
// A5.voice ();
// A5.jump ();

if (A4 instanceof Parrot) {
Parrot A5 = (Parrot) A4;
A5.eat ();
A5.voice ();
A5.jump ();
}

}
}
Summarize
1. The parent class reference can point to the subclass object, and the subclass reference cannot point to the parent class object.
For example: flyanimal flyanimal = new Parrot (); OK
Parrot a7 = new Flyanimal ();
2, the object of the class directly assigned to the parent class reference called upcasting upward transformation, upward transformation without forced transformation.
//
such as Father father = new Son ();//Upward transformation
3. Assign the parent reference of the child class object to the subclass reference called down Transformation (downcasting), to force the transformation.
In Flyanimal flyanimal = new Parrot (), such as Flyanimal is a parent class reference to the Subclass object
, assign Flyanimal to subclass reference Parrot namely Parrot Parrot = (Parrot) Flyanimal;
4, Upcasting will lose the subclass-specific method, but the subclass overriding the method of the parent class, the subclass method is valid.











Java up-conversion (upcasting) and down-conversion (downcasting) Distinction Examples

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.