How to use Java Polymorphism
Editor to share with you how to use Java polymorphism, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Overview of polymorphism
Polymorphism is the third major feature 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 classmate Zhang San is both a student and a person, that is, there are two forms.
As an object-oriented language, Java can also describe multiple forms of a thing. For example, if the Student class inherits the Person class, the object of a Student is both Student and Person.
The definition and usage format of Polymorphism
The definition format of polymorphism: just in time that the reference variable of the parent class points to the subclass object
Parent class type variable name = new subclass type ()
Variable name. Method name ()
A: the format of the definition of general class polymorphism
Parent class variable name = new subclass ()
Characteristics of polymorphic members
A: polymorphic member variables
When a member variable with the same name appears in the child parent class, the polymorphism calls the variable:
Compile time: refer to whether there are called member variables in the class to which the referenced variable belongs. No, compilation failed.
Run time: is also a member variable in the class to which the referenced variable belongs.
Simple note: both compile and run refer to the left side of the equal sign. Compile and run look to the left.
B: polymorphic member method
Compile time: refer to the class to which the reference variable belongs. If there is no method in the class that is not called, the compilation fails.
Run time: refer to the class to which the object referred to by the reference variable belongs and run the member methods in the class to which the object belongs.
In short: compile to the left, run to the right
Upward and downward transition in polymorphism
The transformation of polymorphism can be divided into two types: upward transformation and downward transformation.
A: upward transition: when a subclass object is assigned to a parent class reference, it is an upward transition, and polymorphism itself is a process of upward transformation.
Use format:
Parent class type variable name = new subclass type ()
Such as:
Personp=newStudent ()
B: downward transition: a subclass object that has been transformed upward can convert a parent class reference to a subclass reference using a cast format. This process is a downward transformation. If the parent object is created directly, it cannot be transformed downwards.
Use format:
Subclass type variable name = (subclass type) variable of parent class type
Such as:
Studentstu= (Student) pbank / variable p actually points to the Student object
Advantages and disadvantages of polymorphism
Advantages: it can improve maintainability (guaranteed by polymorphic premise) and code scalability.
Cons: no direct access to members specific to subclasses
The above is all the contents of the article "how to use Java Polymorphism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!