Java Notes Collation (vi), polymorphism in Java

Source: Internet
Author: User

There are two types of Java reference variables: one is a compile-time type, and one is a run-time type

A compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable. If the compile-time type is different from the run-time type, polymorphism is formed.

Because the subclass is actually a special parent class, because Java allows a subclass object to be assigned directly to a parent reference variable, without any type conversion, called upward transformation, is automatically completed by the system.

When a subclass object is assigned directly to the parent class reference variable, when the runtime invokes the method of the reference variable, its method behavior always shows the behavior characteristics of the subclass method, not the behavior characteristics of the parent method, which can occur: variables of the same type, the same method is called to render many different behavior characteristics, which is polymorphic

That is, polymorphism occurs when a subclass object is assigned to a parent class reference variable. At the time of invocation, the property invokes the property of the parent class directly, and if the child class overrides the method of the parent class, the method of the subclass is called, otherwise the parent class method is called, and the subclass's unique method cannot be called.

Forcing type conversions

When writing a Java program, a reference variable can only invoke a method of its compile-time type, not a method of its run-time type, even if the object it references does contain the method. If you need to have this reference variable call a method of its run-time type, you must cast it to the run-time type, forcing the type conversion to take advantage of the type conversion operator.

The type conversion operator is a parenthesis.

Conversions between basic types can only be performed between numeric types, where the numeric types are integer, character, and float. However, there is no conversion between a numeric type and a Boolean type.

Conversions between reference types can only occur between two types that have an inheritance relationship, and if you have two types that do not have any inheritance relationships, you cannot type conversions, or you will compile with an error. If the view transforms a parent class instance into a subclass instance, the object must actually be a subclass instance before the ClassCastException exception occurs.

Use the instanceof operator to make forced type conversions more secure

instanceof Keywords

The previous operand of the instanceof operator is usually a reference-type variable, and the latter operand is usually a class (or an interface) that is used to determine whether the preceding object is a subsequent class, or its subclasses, an instance of the implementation class, and, if so, returns true, otherwise false

** If a subclass overrides a method of a parent class, a null pointer is generated if a method that is overridden by a quilt class is called in the constructor of the parent class

Java Notes collation (vi), polymorphism in Java

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.