Conversion of Java Object types

Source: Internet
Author: User

Import Com.java.charpt05.NewStr;

Class Quadrangle
{
public static void Draw (Quadrangle Q)
{
System.out.println (Q.getclass (). GetName ());
}




}
public class Parallelogram extends quadrangle{
public static void Main (string[] args) {
Parallelogram q=new parallelogram ();
Draw (q);
}




}

This is an upward transformation in object type conversions. It's always safe.

2.

Import Com.java.charpt05.NewStr;

Class Quadrangle
{
public static void Draw (Quadrangle Q)
{
System.out.println (Q.getclass (). GetName ());
}




}
public class Parallelogram extends quadrangle{
public static void Main (string[] args) {
Draw (new parallelogram ());


Quadrangle Q=new Parallelogram ();

Parallelogram q=q; error
Parallelogram p= (parallelogram) q;




}



If the parent class object is given directly to the subclass object, an error occurs because the parent class object is not necessarily an instance of the subclass, and if the parent class object is cast to a child class object, this is the display type conversion.

When using the down transformation technique in a program, you must use display type conversions to indicate to the compiler which type of subclass object The parent object is converted to

Conversion of Java Object types

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.