------polymorphism of three major features of Java

Source: Internet
Author: User

1. Definition

means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object.

2. Conditions of existence

2.1 There is a parent-child relationship

2.2 Overriding methods exist in subclasses

2.3 A variable of the parent class type points to a reference to a subclass object

3. How to Implement

3.1 Interface Implementation

3.2 Inheriting the parent class for method overrides

3.3 Method overloading in the same class

4. Types of casts

4.1 Conversion Format: subclass + New variable = (subclass) + variable (variable reference of parent class type to child class object)

4.2 A variable of the parent class type can be converted to a variable of the subclass type, but a "type conversion exception" may occur

4.3 using x instance A to determine the exception, requires that x belongs to Class B and class A is a parent-child relationship: The result is true, does not throw an exception

5. Example Analysis

Example one:

Class A {

        public String Show (A obj) {
             return ("A and a");
        }
        public String Show (D obj) {      //method
            return ("A and D");
        }      
    }

Class B extends A {
Public String Show (B obj) { //overload of Method
Return ("B and B");
}
Public String Show (A obj) { //override of method
Return ("B and A");
}
}

Class C extends B {
}

Class D extends B {
}

public class Duotai {
public static void Main (String args[]) {
A A1 =new a ();
b b = new B ();
c C = new C ();
D d = new D ();
//polymorphic: A variable of the parent class type points to a reference to a subclass object
A A2 =new B ();

//polymorphic Representations: Variables of the same type, calling the same method, produce a different result.
System.out.println (A1.show (b));

A1.show (b):

1. First look at the type of the reference variable A1, for a, B is an instance of B.

2. In Class A see if there is a method show (B obj), the discovery does not exist;

3. See if A has a superclass (parent class), and the discovery does not exist;

4. See if there is a method in a show (a obj), exists;

Note: The parameter B is an instance of B, B is a subclass of a, the parameter B is converted to a and then the method is found, in a find out if there is a method that B can inherit show (A obj)

5.A1 is a reference to its own object, so the rewrite of the method is not considered , and the answer is a and a.

S Ystem.out.println (A1.show (c));

A1.show (c):

The same

Method Show (B obj) is not present in 4.A; proceed to step 3, no; continue to step 4, Class A has show (a obj);

5. Ibid.
System.out.println (A1.show (d));

System.out.println (A2.show (b));

A2.show (b):

The same

4.A If there is a method show (a obj), exists;

Note: convert parameter B to a

5.A2 is a reference to a sub-Class B object, considering the overriding of the method, which overrides the Class A's show (a obj) in subclass B, so the answer is B and a.
System.out.println (A2.show (c));
System.out.println (A2.show (d));

System.out.println (B.show (b));

System.out.println (B.show (c));

B.show (c):

Ditto

Whether there is a method show (c obj) in the parent class A of 3.B, does not exist;

4.B If there is a method show (B obj), exists;

5.B is a reference to the object itself, regardless of the rewrite of the method, and the answer is B and B.


System.out.println (B.show (d));
}
}

Example two:

public class Person {
public void Walk () {
System.out.println ("Walking");
}
}
public class Mans extends person{
public void Walk () {
System.out.println ("Men Trot Away");
}
public void work () {
System.out.println ("Man is a workaholic");
}
}

public class Woman extends person{
public void Walk () {
SYSTEM.OUT.PRINTLN ("Woman walks Away");
}
public void Shopping () {
System.out.println ("Woman is a Shopaholic");
}
}
public class Test {
public static void Main (String args[]) {
Person P1=new person ();

//polymorphic: A variable of the parent class type points to a reference to a subclass object
Person P2=new Mans ();
Person p3=new Woman ();

//polymorphic Representations: Variables of the same type, calling the same method, produce a different result.
P1.walk ();
virtual method invocation : When the method of the parent class is overridden, the method that the subclass overrides is called, not the method of the parent class.
P2.walk ();
P3.walk ();

in polymorphic situations, only methods of the parent class and methods overridden by the quilt class can be called, and if a method of calling a subclass is called, a pre-cast of type is required
//P2.work (); Error


Man m= (man) P2;
m.work ();
Woman w= (Woman) P3;
W.shopping ();

a variable of the parent class type can be converted to a variable of the subclass type, but a "type conversion exception" may occur.
/*woman w= (Woman) P2;
w.shopping (); */

/*instanceof operator
x instance A, requires that x belongs to Class B and class A is a parent-child relationship
the instanceof can detect whether a type cast exception is thrown, and returns true to indicate that the type can be converted * /
System.out.println (P2 instanceof Mans);
}
}

------polymorphism of three major features of 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.