Java Object-oriented Note 4

Source: Internet
Author: User

Encapsulation, inheritance, polymorphism are the three main features of object-oriented, what is polymorphism in Java?


There are two types of Java reference variables, the compile-time type and the run-time type, respectively. The 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.


, that is, if the compile-time type and run-time type are inconsistent, the polymorphism mentioned above may occur.


Let's look at the following procedure:

Class BaseClass {public String name = "BaseClass";p ublic void Base () {System.out.println ("method of the parent class");} public void Test () {System.out.println ("method of the parent class, will be overridden by the Quilt class later");}} public class Main extends BaseClass {public String name = "Main";p ublic Void Test () {System.out.println ("subclass overrides method of parent class");} public void Main () {System.out.println ("method of Subclass");} public static void Main (string[] args) {BaseClass MainClass = new Main ();//1system.out.println (mainclass.name);// 2mainclass.test ();//3//mainclass.main ();//4 (Error)}}

A parent class BaseClass is defined above, the subclass main inherits the parent class BaseClass, and the parent class's method test method is overridden in the subclass.

OK, first look at the output results:

BaseClass
Subclasses overriding parent class methods

From the above program can be quickly seen when the object is created, compile-time type (BaseClass) and run-time type (main) is not the same, when the subclass Main overrides the parent class's test method, call the reference variable's test method, the actual


When you execute the test method that is covered in the main class, polymorphism occurs. Subclasses are a special kind of parent class, so Java allows a subclass object to be assigned directly to a parent class reference variable without any type conversion, or by being called


For upward transformation, the upward transformation is done automatically by the system. The compile-time type of the MainClass reference variable above is BaseClass, and the run-time type is main, and when the runtime invokes the method of the reference variable, its method behavior is always the table


The behavior characteristics of a subclass method, rather than the behavior characteristics of the parent method, can occur: variables of the same type, calls to the same method are presented in a variety of different behavioral characteristics, which is polymorphic. In code 4 above, this line


The code will be wrong at compile time, because it is compiled with a type of baseclass, so the main method cannot be called.


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, or, if it is necessary to have this reference variable invoke the method of its run-time type, it can be cast to


The run-time type. There are a few things to be aware of when forcing type conversions:


1. Conversions between primitives can only be performed between numeric types, including integer, character, and floating-point types. It is important to note that there is no type conversion between a numeric type and a Boolean type.


2. Conversions between reference types can only be done between two types that have an inheritance relationship.


Before you perform a type conversion, it is generally advisable to use the instanceof operator to determine whether a successful conversion is possible to avoid classcastexception exceptions. instanceof usage is as follows

if (str instanceof String) {

String s= (String) str;

}

The previous operand of the instanceof operator is usually a reference-type variable, and the latter operand is usually a class or an interface.





Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43868569 Emotional Control _  





Java Object-oriented Note 4

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.