Java-Object-oriented chapter (supplementary)

Source: Internet
Author: User

1. Polymorphism

Encapsulation is an operation to protect a property, and inheritance is to extend the functionality of a class

Polymorphism is another feature of the object-oriented programming language that is derived from encapsulation and inheritance.

Polymorphic manifestation: Overloading and rewriting of methods

1.1 Static polymorphism (compile-time polymorphism): implemented by method overloading (overload)

1.2 Dynamic polymorphism (run-time polymorphism): Subclass objects can be used as parent class objects, with method overrides (Overridder) implementations

2. The polymorphism of the object

There are two main types of polymorphism of objects:

1. Upward Transformation:

Parent class Parent Class object = subclass instance;//program auto-complete

Note the usage rules:

The parent class object is manipulated , so the newly added members (properties and methods) of the child class cannot be accessed.

Methods overridden in subclasses and member variables with the same name

A. member variables (including static variables) and static member methods: Compile and run look to the left (see parent class)

B. Non-static member method: Compile to see the left side, run to the right (the subclass overrides the method).

2. Downward transition:

Subclass Subclass Object = (subclass) Parent class object;//need to cast

Java-Object-oriented article (supplemental)

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.