Java Fundamentals Section----4. Transformation of objects

Source: Internet
Author: User

Object-oriented three features: encapsulation, inheritance, polymorphism

This section is the embodiment of object-oriented polymorphism

1. Upward transformation of objects

The upward transformation----assigns the object of the subclass to a reference to the parent class;

Student s = new Student ();

Person p = s;

Or it can be written

Person p = new Student ();

What members (variables and functions) can be called by a reference, depending on the data type of the application

Which method a reference invokes depends on the object to which the reference refers (the member variables and methods that are called subclasses)

2. Object-Down transformation

Transition down----Assign the object of the parent class to a reference to the subclass:

Student S1 = new Student ();

Person p = S1;

Student s2 = (Student) p;

The directly generated parent class cannot be transformed into a subclass object;

Java Fundamentals Section----4. Transformation of 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.