The polymorphism of the Java language

Source: Internet
Author: User

Describe Java in simple words: Polymorphism occurs when the compilation type is inconsistent with the run type!

The following section of code can be used to describe the Java polymorphic

class baseclass{public String flag= "Flag of Parent class";p ublic BaseClass () { System.out.println ("The parent class is called without a parameter constructor");} public void Base () {System.out.println ("base method for parent class");} public void Test () {System.out.println ("the test method of the parent class");}} Class Subclass extends Baseclass{public string flag= "flag of Subclass";p ublic string flag2= "Subclass Flag2";p Ublic subclass () { System.out.println ("sub-class parameterless constructor");} public void Test () {System.out.println ("test method for Subclass");} public void Exam () {SYSTEM.OUT.PRINTLN ("exam method of the subclass");}} public class Test{public static void Main (string[] args) {baseclass bc=new baseclass (); System.out.println (Bc.flag); bc.test ();//output://Parent Class no parameter constructor is called//parent class flag//Parent class of the test method subclass Sc=new subclass (); System.out.println (Sc.flag); Sc.test (); Sc.exam ();//output://Parent non-parametric constructor called//subclass of Flag//Subclass parameterless constructor//subclass of test method// The exam method of the subclass BaseClass X=new subclass (); System.out.println (X.flag);//system.out.println (X.FLAG2);//error x.base (); X.test ();//x.exam ();//The error does not define a method for the Type BaseClass exam ()//output://Parent Class no parameter constructor is called//subclass parameterless Constructor//parent class of flag//parent class base method//Subclass of test Method}} 

In the above code, the compilation type of object instance X is BaseClass and the run type is subclass
At the time of operation

One, the method that the instance object can call to satisfy
1, the method must be the parent class (that is, the compilation type) contains the method, the subclass (that is, the run type) of the unique method (the newly added method) can not be called.
2, when the method is called, it is shown that the characteristics of the subclass method (such as subclasses of the parent class to overwrite the method, the output changes after the content), or still output the original content of the parent class

  Second, the principle of invoking variables for instance objects
    
1, the same as above the first article
2. When accessing the variable, the variable of the parent class is always accessed, not the variable of the child class, as opposed to the 2nd of the calling method.

The polymorphism of the Java language

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.