Object-oriented polymorphism of Java learning notes

Source: Internet
Author: User

The polymorphism of the three main characteristics of object-oriented one. The concept of polymorphism

Polymorphism is the three main characteristics of object-oriented after encapsulation and inheritance.

Real things often reflect a variety of forms, such as students, students are a kind of people, then a specific Zhang San students are students are also people, that appears two forms.

As an object-oriented language, Java can also describe the various forms of a thing, and the polymorphic code in Java is embodied in a subclass object (Implementation class object) that can assign values to the subclass (Implementation class object) reference variable, and to the parent class (interface) variable of the subclass (Implementation class object).

  The reference variable of the parent class points to the child class object. A polymorphic premise requires an inheritance relationship or a realization relationship, otherwise it is impossible to complete polymorphism. When a parent class referencing a variable that uses polymorphism, the subclass is called to rewrite the method;

two. Definition and use of polymorphism

Polymorphic definition Format: Is the reference variable of the parent class that points to the object of the subclass;

Parent class type variable name = new Subclass Object ();

Variable name. Method name ();

1. Common class of polymorphic definition format:

Parent class type variable name = new Subclass ();

1  Public class Fu {}
1  Public class extends  fu{}
1 // Polymorphic Use 2 Fu  new Zi ();

2. Definition of abstract polymorphic polymorphism:

Abstract class variable name =new abstract class sub-class ();

// Abstract class  Public Abstract  class   fu{  publicabstract  void  method ();  
// sub-class  Public class extends fu{   publicvoid method () {// Override abstract method}}
// Use of polymorphic Fu f=New Zi ();

3. The format of the interface polymorphism definition:

Interface variable name =new interface implementation Class ();

  

Interface
publicInterface fu{ publicabstractvoid method ( );}
// Implementation Class  Public Zi  implements  fu{   publicvoid  method () {       // overriding an abstract method of an interface     }}
//  polymorphic Fu  f=new  Zi ();

Note: Although the variable points to the object of the subclass, but behaves as a form of a parent class, you can call all methods of the parent class, and the subclass-specific method cannot invoke the

Three. Considerations for polymorphic Calls:

Compile: Member variable, the member method all need to look at the parent class, the parent class does not compile the error;

Run: Member variable runs a member variable of the parent class, and the member method runs the overridden method of the parent class

Four. The benefits and drawbacks of polymorphism

Benefits: Multi-state matching inherits subclass rewrite, which improves the reusability and extensibility of code

Cons: You can't call a subclass-specific method

Five. Up-down type conversion

Polymorphism itself is the process by which the subclass type is transformed upward into the parent class type;

1. Upward Transformation:

When an object of a subclass is assigned a reference to a parent class, it is an upward transformation, and polymorphism itself is the process of upward transformation

Use format: parent class type variable name = new subclass type ();

2. Downward transition:

A child class object that has been transformed upward can use the format of coercion type conversion to convert a reference from a parent class to a subclass reference, which is a downward transformation. The direct creation of the parent class object cannot be used down transformation;

Use format:

    Subclass type variable name = (subclass type) A variable of the parent class type;

Six. instanceof keyword

The instanceof keyword can be used to determine whether an object belongs to a data type;

Use format:

     The Boolean b= object (new) instanceof data type;

Object-oriented polymorphism of Java learning notes

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.