Transformation of the "seven" objects

Source: Internet
Author: User

An object of a subclass is transformed upward to the object of a parent class, and the properties and methods that are called in the parent class are the child class objects that are pointed to.

That is, the method that points to the replication override in the subclass.

The upward transformation is not likely to fail.

//What members (variables and functions) can be called by a reference, depending on the type of the reference//which method is called by a reference, depending on the object to which the reference is directedclasstest{ Public Static voidMain (String args []) {//Student s = new Student (); //Person p = s;Person p =NewStudent ();//Upward TransformationStudent s = (Student) p;//downward transformation must first be transformed to a downward transformation, or the wrong transformationP.name="Zhangsan"; P.age= -; //The wrong p.address = "Beijing";p.introduce (); }}
class person{    String name;     int Age ;         void introduce () {        System. out. println ( "+ name +"" + Age" );    }    }
class Student extends person{    String address;     void Study () {        System. out. println (" I'm learning ");    }     void introduce () {        super.introduce ();        System. out. println (" my home is in "+ address);}            }

Transformation of the "seven" 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.