The polymorphism of Java objects

Source: Internet
Author: User

There are two main types of polymorphism in object-oriented:

Overloading and overwrite of <1> methods

<2> Polymorphism of objects

Object polymorphism: Transform up: Subclass Object----parent object, upward transformation is done automatically

Transition down: The parent object, and the subclass object, must explicitly indicate the transformed subclass type

An upward transformation of an object

Although the parent class object that is used calls the Fun1 method, the method that is actually called is a method overridden by the quilt class, that is, if the object has an upward transformation relationship, the method that is called must be a method overridden by the quilt class.

A parent class, however, cannot invoke the Fun3 method in class B because the method is defined only in the subclass, not in the parent class.

=================================================//File Name       :p loy_up_demo//----------------------------- -------------------------------------------------///Author          : common//class Name: c_1//attribute://Method: Class c_1{//define Interface c_ 1public void Fun1 () {//define FUN1 () method System.out.println ("C_1--->public void fun1");} public void Fun2 () {//define FUN2 () method This.fun1 ();}} Class Name: d_1//property://Method: Class D_1 extends c_1{//subclass D_1 inherit parent class C_1public void Fun1 () {System.out.println ("D_1--->public void Fun1 "),//Overwrite the FUN1 () method in the parent class}public void Fun3 () {System.out.println (" D_1--->public void fun3 ");//Subclasses define methods}}//main class// Function        : Object polymorphism, object is transformed public class Ploy_up_demo {public static void main (string[] args) {//TODO auto-generated method stub d_1 b = NE W d_1 ();//defines the instantiation object of a subclass c_1 a = b;//declares a parent class, an upward-transition relationship occurs, the subclass assigns a value to the parent class a.fun1 ();//This method is overridden by the quilt class, although a is the parent class, but the Fun1 () method of the subclass is called}}

Object-down transformation

In the subclass called the Fun2 method in the parent class, the Fun2 method calls the Fun1 method, but at this time the Fun1 method has been overridden by the quilt class, so call the Fun2 method is called Quilt class overridden method

Before the object's downward transformation, the object must first be transformed, or an object conversion exception will occur

=================================================//File Name       :p loy_up_demo//----------------------------- -------------------------------------------------///Author          : common//class Name: c_1//attribute://Method: Class c_1{//define Interface c_ 1public void Fun1 () {//define FUN1 () method System.out.println ("C_1--->public void fun1");} public void Fun2 () {//define FUN2 () method This.fun1 ();}} Class Name: d_1//property://Method: Class D_1 extends c_1{//subclass D_1 inherit parent class C_1public void Fun1 () {System.out.println ("D_1--->public void Fun1 "),//Overwrite the FUN1 () method in the parent class}public void Fun3 () {System.out.println (" D_1--->public void fun3 ");//Subclasses define methods}}//main class// Function        : Object polymorphism, object is transformed public class Ploy_up_demo {public static void main (string[] args) {//TODO auto-generated method stub//d_1 B = New D_1 ();//defines the instantiation object of a subclass//c_1 a = b;//declares a parent class, an upward-transition relationship occurs, the subclass assigns a value to the parent class//a.fun1 ();//This method is overridden by the quilt class, although a is the parent class, but the Fun1 () method of the subclass is called c_1 C = New D_1 ();//declares a parent class that has undergone an upward transformation, the subclass is assigned to the parent class d_1 D = (d_1) c;//The parent of the declaration is cast to the subclass, and the downward Transformation relationship d.fun1 ();d. fun2 ();d. FUN3 ();}

Polymorphism of Java objects

Related Article

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.