Implementation of Java runtime polymorphism

xiaoxiao2021-03-06  88

Implementation of Java runtime polymorphism

China Construction Bank Zheng Zhiyuan

Runtime polymorphism is a most powerful mechanism for object-oriented programming code reuse. The concept of dynamicity can also be said to be "an interface, multiple methods." The basis of Java realizes the runtime polymorphism is a dynamic method scheduling. It is a mechanism for running instead of compile time calling overloading methods. Here, inheritance and interface implementation talks about Java runtime polymorphism .

1. Implementation by inheriting the super-class object reference variable reference subclass object

for example:

/ / Define super class Supera

Class Supera

{

INT i = 100;

Void fun ()

{

System.out.println ("this is supera");

}

}

/ / Define SUPER's subclass SUBB

Class Subb Extends Supera

{

INT m = 1;

Void fun ()

{

System.out.Println ("this is subb");

}

}

/ / Define SUPER's subclass SUBC

Class Subc Extends Supera

{

INT n = 1;

Void fun ()

{

System.out.Println ("this is subc");

}

}

Class test

{

Public static void main (string [] args)

{

Supera a;

Subb b = new subb ();

SUBC C = New Subc ();

A = B;

a.fun (); (1)

A = C;

a.fun (); (2)

}

}

The result of the operation is:

This is subb

This is subc

Subb and SUBC in the above code are subclass of superclass SUPERA, we declare 3 reference variables A, B, C in class test, and dynamic method calls are realized by reference to the sub-object reference variable to super-class object reference variables. Maybe someone will ask: "Why (1) and (2) do not output: this is supera. This mechanism of Java follows a principle: When the hypercoat object reference variable reference subclass object, the type of object is cited rather than the type of the reference variable determines the member method of calling who is called, but this method of call must be super The method defined in the class is that the method covered by the subclass.

Therefore, don't be confused in the previous example, although it is written as A.fun (), but since A in (1) is assigned by B, pointing to one instance of sub-Subb, therefore (1) The called FUN () is actually a member method of the sub-SUBB (), which covers the members of the superclass SUPERA (); the same (2) called the members of the subclass SUBC Fun ().

Also, if the superclass inherited by the subclass is an abstract class, although the abstract class cannot be unitened by the New operator, you can create an object reference to the subclass of the abstract class to achieve the runtime polymorphism. Specific realization method is inheed.

However, the subclass of abstract classes must cover all abstract methods in the superclass, otherwise the subclass must be modified by Abstract modifiers, of course, can not be instantiated.

Second, the interface type variable references the object to implement the interface of the interface is implemented.

The flexibility of the interface is "" What must be done in a class, regardless of how you do ". We can define a reference variable for an interface type to reference an instance of the class that implements the interface. When this reference call method, it determines which method of the specifically call according to the instance of the actual reference class, this and the above-mentioned superclass object reference Access The mechanism of sub-objects is similar.

for example:

/ / Define Interface Intera

Interface intera

{

Void fun ();

}

/ / Implement interface INTERA class B

Class B IMPLEMENTS INTERA {

Public void fun ()

{

System.out.println ("this is b");

}

}

/ / Implement interface interaity class C

Class c imports intera

{

Public void fun ()

{

System.out.println ("this is c");

}

}

Class test

{

Public static void main (string [] args)

{

Intera A;

a = new b ();

a.fun ();

a = new c ();

a.fun ();

}

}

The output is:

THIS IS B

This is c

In the above example, class B and class C are two classes that implement interface interane, and the method of interface is implemented, respectively, and the method is implemented by the implementation of the class B and class C to the interface reference A. Binding, fully utilizing "an interface, multiple methods" showing the dynamic polymorphism of Java.

It should be noted that when Java calls its real-creating objects using the interface variable, the method must have been declared in the interface, and the type and parameter of the implementation method in the interface implementation must be in the interface. Exactly matching definition.

Conclusion: The above is a method of implementation of the Java runtime. Everyone can be flexible during the programming process, but do not promote the use of running polymorphisms in the code with higher performance requirements, after all, Java runtime dynamic method calls The general method called system overhead is relatively large.

转载请注明原文地址:https://www.9cbs.com/read-106050.html

New Post(0)