Using Java to implement object-oriented chapter III

Source: Internet
Author: User
Chapter III polymorphism One, writing parent-child class

1. Polymorphism is characterized by the ability to express multiple types of ecology, the same implementation interface, the use of different instances to perform different operations

2. A reference type that performs different operations with different instances. (The parent class refers to the child class object)

Benefits of using polymorphism: by polymorphism you can reduce the amount of code in a class, which can improve the scalability and maintainability of your code.

3. Implementing ideas to write subclasses, subclasses overriding parent class methods

When run, uses the parent class's type, the subclass of the object.

Ii. two elements to achieve polymorphism

1. Rewrite

Using the parent class type

Rules for method overrides

In the subclass of the inheritance relationship

The overridden method name, parameter, and return value type must be the same as the parent class

Private methods are not tender tricks and thus cannot be rewritten.

2.Instanceof operator

Instanceof class or interface

This operation is used to determine whether an object belongs to a class or implements an interface. The result is true or false.

Rules when a subclass is converted to a parent class:

Transformation of subclass to parent class (up conversion)

Conversion of parent class to subclass (down conversion)

Using Java to implement object-oriented chapter III

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.