Instance viewing Polymorphism

Source: Internet
Author: User
1. Image Understanding
Two principles of understanding:
(1) A derived class object can be declared as a base class, or a base class pointer can point to a derived class object:
// C ++ code

Baseclass * P;

Derivedclass OBJ;

P = & OBJ;

// C # code

Baseclass OBJ = new derivedclass ();

(2) Think of an object as an independent individual. Calling the object's Public member function actually sends a message to the object. The action taken is completely determined by the object itself.

Shape is a base class, and circle and line are inherited from shape. shape has the draw () method, and circle and line define their own draw () methods respectively.CodeIn:

// Java code

Static void func (shape S)

{

S. Draw ();

}

If such a call occurs:

Line L = new line ();

Circle C = new circle ();

Func (L );

Func (C );

A circle and a line are uploaded to the function as shapes, and then draw () is called. What happens? Because the objects are independent individuals, in func (), the two objects are respectively passed the draw () message, so that they can draw themselves, so they respectively called the draw () action defined in their own classes.

Through these two principles, we can understand the polymorphism above. It is precisely because of polymorphism that we do not need to do this:

If you are a circle then, call the draw () of the circle ()

Else if you are a line then call line draw ()

Else...

We only need to send a draw message to the object declared as a shape. It is up to the object to determine how to perform draw.

2. All functions are virtual.

First, let's look at the basic conditions for implementing polymorphism:

(1) The base class contains virtual functions

(2) The Inheritance class implements this virtual function again.

(3) The Inheritance class may not re-implement all the virtual functions of the base class. Therefore, no polymorphism is allowed for these unre-implemented virtual functions.

Let's take a look at some special rules in several languages:

1. c ++:
(1) virtual functions are declared with virtual keywords.
(2) virtual void func (para_list) = 0; such a virtual function is called a pure virtual function, indicating that this function has no specific implementation. A class that contains pure virtual functions is called an abstract class. If its inheritance class is not specifically implemented by code for this pure virtual function, this inheritance class is also an abstract class. Abstract classes cannot be used by instances (that is, they cannot be created ).
(3) When the inheritance Class re-implements the virtual function of the base class, no special declaration is required.
(4) If you do not need to modify the virtual keyword and re-implement this method in the derived class, this is just a simple overwrite and will not produce polymorphism. For now, we call it non-polymorphism.

2. Java:
(1) Java does not have the virtual keyword. Java regards all methods of classes as virtual functions.
(2) When the inheritance Class re-implements the virtual function of the base class, no special declaration is required. Therefore, as long as the methods of the base class are re-implemented in Java and the inherited class object is declared as the base class, polymorphism will occur. Therefore, Java has relatively low polymorphism conditions.

// Java code
Class baseclass
{
Public void Hello (){};
}

Class derivedclass extends baseclass
{
Public void Hello ()
{
System. Out. println ("Hello World !");
}

Public static void main (string ARGs [])
{
Baseclass OBJ = new derivedclass ();
OBJ. Hello ();
}
}

The input is Hello world !. In this way, polymorphism is achieved.

(3) virtual functions are declared with abstract statements. Classes containing virtual functions are abstract classes and must be modified with abstract keywords.

// Java code
Public abstract abstractclass
{
Public abstract void Hello ();
//...
}

3. C #:
C # Writing polymorphism is the most rigorous and rigorous.
(1) virtual functions are declared using virtual.
(2) pure virtual functions are declared using abstract statements. Classes containing pure virtual functions are abstract classes and must be modified using abstract keywords.
(3) If it is only a non-virtual method that overrides the base class, you need to declare it with the New Keyword:
// C # code
Public class baseclass
{
Public void Hello ()
{
System. Console. writeline ("hello, this come from baseclass ");
}
}

Public class derivedclass: baseclass
{
Public new void Hello ()
{
System. Console. writeline ("Hello, this is come from derivedclass ");
}

Public static void main ()
{
Baseclass OBJ = new derivedclass ();
OBJ. Hello ();
}
}

The output is hello, this come from baseclass, which means no polymorphism (non-polymorphism) is implemented ).

(4) realize polymorphism by combining virtual-override and abstract-override.
When the derived class re-implements the virtual function (or pure virtual function) of the base class, the override keyword must be used for modification.

// C # code
Public abstract class absbaseclass
{
Public abstract void Hello ();
}

Public class derivedclass: absbaseclass
{
Public void Hello ()
{
System. Console. writeline ("Hello World !");
}

Public static void sayhello (absbaseclass OBJ)
{
OBJ. Hello ();
}

Public static void main ()
{
Derivedclass _ OBJ = new derivedclass ();
Derivedclass. sayhello (_ OBJ );
}
}

Output is Hello world!

Iii. Trace of Polymorphism
When the inherited class object is muttered, it does not leave the base class alone. It will view the list of virtual functions of the base class, And the polymorphism will only occur within the scope of this list.
Let's look at a complicated example:

// Java code
Class

{

Protected void Hello (Object O)

{

System. Out. println ("A-object ");

}

}

Class B extends

{

Protected void Hello (string S)

{

System. Out. println ("B-string ");

}

Protected void Hello (Object O)

{

System. Out. println ("B-object ");

}

};

Class C

{

Public static void main (string ARGs [])

{

Object OBJ = new object ();

String STR = "ABC ";

A A = new B ();

A. Hello (OBJ );

A. Hello (STR );

}

};

Output result:

B-Object

B-Object

As mentioned above, because the base class does not have a virtual function whose parameter type is string, the hello (string) method of B is not involved in polymorphism. Call. hello (STR), because string is the inheritance class of the object, this STR is passed into B's hello (object) as an object, as described in principle 1.

4. Interfaces-only abstract classes

The interface is a class protocol, but because the interface is also involved in polymorphism, from this point of view, we think it is a more abstract class, as shown below:

// Java code
Interface IBASE

{
Void Hello ();
}

Class derivedclass implements IBASE
{
Public void Hello ()
{
System. Out. println ("Hello World !");
}

Public static void main (string ARGs [])
{
IBase OBJ = new derivedclass ();
OBJ. Hello ();
}
}

In Java and C #, a class can only be derived from one base class, but multiple interfaces can be implemented.

Here is a small problem: if both ibase1 and ibase2 declare a hello () method, derivedclass implements these two interfaces. Of course, hello () needs to be implemented.

Interface ibase1
{
Void Hello ();
}

Interface ibase2
{
Void Hello ();
}

Public class derivedclass1: ibase1, ibase2
{
Public void Hello ()
{
System. Console. writeline ("Hello World !");
}
}

Public class derivedclass2: ibase1, ibase2
{
Void ibase1.hello ()

{
System. Console. writeline ("This come from ibase1 ");
}

Void ibase2.hello ()
{
System. Console. writeline ("This come from ibase2 ");
}

Public static void main ()
{
Ibase1 obj_1 = new derivedclass1 ();
Ibase2 obj_2 = new derivedclass1 ();
Ibase1 obj_3 = new derivedclass2 ();
Ibase2 obj_4 = new derivedclass2 ();

Obj_1.hello ();
Obj_2.hello ();
Obj_3.hello ();
Obj_4.hello ();
}
}

Output:

Hello world!

Hello world !;

This come from ibase1

This come from ibase2

Note: (1) the implementation method of derivedclass2 is called explicit implementation. This method is supported by C # and cannot be implemented in Java. (2) further tests show that the hello () method does not belong to derivedclass2:

Add such code

Derivedclass2 T = new derivedclass2 ();

T. Hello ();

Compilation error: Test. CS (): Error cs0117: "derivedclass2" does not include the definition of "hello"

That is to say, this method belongs to the interface, but the interface cannot contain specific implementation code. Is there a conflict here?

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.