Java Multi-State knowledge summary + Simple code example

Source: Internet
Author: User

first, before introducing polymorphism, introduce an important knowledge point--java type of reference variable

There are two types of reference variables in Java:

1. Compile-time type , which is determined by the type used when declaring the variable;

2, the Run-time type , by the actual assigned to the variable of the object decision;

When the compile-time type is inconsistent with the Run-time type, it can occur-polymorphic


second, what is polymorphism

Polymorphism is "multiple forms"; in the process of running the program to decide which method to use, polymorphism is a technique that allows you to set the parent object to be equal to one or more of its child objects, and when assigned, the parent object can operate according to the attributes of its children that are currently assigned to it, which is polymorphic.

Polymorphism is the core concept in object-oriented programming field. (Object-oriented three characteristics: encapsulation, inheritance, polymorphism)

Note: Overloaded methods are determined during compilation and are static , method overloads are independent of polymorphism, and real polymorphism-related overrides (overlay override);


third, the upward transformation

A subclass is a special kind of parent class that allows a subclass object to be assigned directly to a parent class reference variable without type coercion, called an upward transition, which is done automatically by the system.


the necessary conditions for the formation of polymorphism

1, to have inheritance

2, to have rewrite (cover override)

3, the subclass object is assigned to the parent class reference variable (the parent class reference points to the subclass object)


Five, polymorphic code instances

Package com.java.test;
	Class Father {public String name = ' Father '; The parent class basic method public void Fathermethod () {System.out.println () is the basic method of the parent class.
	"); //Quilt class overrides method public void Overridemethod () {System.out.println ("Parent Quilt class overrides method.")
	");
	The public class child extends father{public String name = ' child '; Subclass Basic Method public void Childmethod () {System.out.println ("subclass basic method.")
	"); The//subclass overrides the parent class method public void Overridemethod () {System.out.println () the subclass overrides the parent class method.
	");
		The public static void main (string[] args) {//The following compile-time type is the same as the Run-time type, and there is no polymorphic Father Father = new Father ();
		System.out.println (Father.name);
		Father.fathermethod ();
		Father.overridemethod ();
		System.out.println ();
		The following compile-time type is the same as the Run-time type, and there is no polymorphic child child = new Child ();
		System.out.println (Child.name);
		Child.childmethod ();
		Child.overridemethod ();
		System.out.println (); The following compile-time types are different from run-time types, where polymorphic occurrences//parent class references point to subclass objects, polymorphic conditions are complete//father2 object compile-time type is Fatherclass, runtime type is childclass Father father2 = new Chil
		D (); Note: Unlike methods, the field of an object does not have polymorphism, and the following output is the parentclass's name System.out.println (Father2.name);
		Father2.fathermethod ();
	Polymorphic occurrences, where the subclass rewrite method is called Father2.overridemethod (); }
}




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.