Java/C # Polymorphism

Source: Internet
Author: User

My recent interview has been severely affected and I feel that my internal skills are insufficient. So I looked at java programming ideas and gained a deeper understanding of polymorphism.

In the past, I only knew what is the use of polymorphism and how to use it, but I don't know the principle of polymorphism. Now I want to know it and I don't want to hide it. I want to share it with you, you don't need to watch it.

The principle of polymorphism is"Bind method call later".

 

What is post-binding?

It is called binding to associate a method call with a method subject. If it is bound before the program is executed (for example, during compilation), it is called pre-binding (C language is pre-binding ). Correspondingly, binding Based on the object type during runtime is called post-binding, also called dynamic binding. That is to say, if a language wants to implement later binding, it must have a mechanism to determine the object type at runtime and call appropriate methods.That is to say, the compiler never knows the object type, but the method calling mechanism at runtime can find the correct method body and call it. Either way, some type of information must be placed in the object.

In java, except the static method and the final method (the private method belongs to the final method), all other methods are bound later by default (that is, they can be polymorphism ).If you declare a method as final, static, or private, the compiler does not need to dynamically bind the method. In this way, the compiler can generate more effective code () for final method calls ().

Summary:

1. By default, common java methods are dynamically bound.

2. The static and final methods of the parent class (except for private) cannot be overwritten by subclass. The Compiler reports an error. The constructor is static by default.

3. the parent class private (also the final method by default) method. The subclass can declare the same name method (this method and the parent class same name method only have the same name, it does not matter), but it does not have polymorphism, it is also impossible to be polymorphism, because the parent class method is private.

4. the visible range (protected, public, etc.) of the method that requires polymorphism in the subclass cannot be smaller than that of the parent class. Otherwise, the parent class may not be able to find the method of the subclass, an error is reported during compilation.

5. java attributes cannot be polymorphism. Polymorphism is only applicable to methods.

The following is an example.

1 class Base {2 3 public int a = 2; 4 public void fmethod () {5 System. out. println ("base: fmethod"); 6} 7 int getA () {8 return a; 9} 10} 11 12 class Sub extends Base {13 public int a = 3; 14 public void fmethod () {// final indicates not all polymorphism 15 System. out. println ("sub: fmethod"); 16} 17 // The visible range of the subclass method can be expanded by 18 public int getA () {19 return; 20} 21} 22 public class Test {23 24 public static void main (String args []) {25 Base B = new Sub (); 26 // method polymorphism, output sub: fmethod27 B. fmethod (); 28 // the property cannot be polymorphism and the output is 2.29 System. out. println (B. a); 30 // but the property polymorphism can be achieved through the method, and 331 System is output. out. println (B. getA (); 32} 33 34}

 

C # most of the polymorphism mechanisms in java are the same, but there is a slight difference.

Different:By default, java common methods are bound later, while C # methods are bound earlier by default.Therefore, if you need to bind (polymorphism) in C #, you must use the virtual + override + overide keyword pair. On the other hand, it seems that the access restrictions for methods that require polymorphism (override modification in this article) must be consistent. If they are inconsistent, an error is returned. It is unclear whether this statement is correct.

A few gossips, code

1 class Base {2 3 public int a = 2; 4 public void fmethod () {5 Console. writeLine ("base: fmethod"); 6} 7 // virtual keyword. You can prompt the compiler that this method can be rewritten by the subclass (The subclass method must have override ), 8 public virtual void gmethod () 9 {10 Console. writeLine ("base: gmethod"); 11} 12 13 public virtual void hmethod () 14 {15 Console. writeLine ("base: hmethod"); 16} 17 protected virtual int getA () 18 {19 return a; 20} 21} 22 23 class Sub: Base24 {25 public I Nt a = 3; 26 // The default modifier for common methods is new and public void fmethod = public new void fmethod27 public void fmethod () {28 Console. writeLine ("sub: fmethod"); 29} 30 // With override, paired with the parent class virtual, polymorphism 31 public override void gmethod () 32 {33 Console. writeLine ("sub: gmethod"); 34} 35 // even if the parent class method has virual, The subclass method does not override and is not polymorphism. 36 public void hmethod () 37 {38 Console. writeLine ("sub: hmethod"); 39} 40 41 42 // The method access restrictions for subclass rewriting must be consistent, right? 43 protected override int getA () {44 return a; 45} 46} 47 48 class Subsub: Sub49 {50 // override exists, paired with the parent class virtual, polymorphism 51 public override void gmethod () 52 {53 Console. writeLine ("subsub: gmethod"); 54} 55 public static void Main (String [] args) 56 {57 Base B = new Sub (); 58 // output base: fmethod59 B. fmethod (); 60 // output sub: gmethod61 B. gmethod (); 62 // output base: hmethod63 B. hmethod (); 64 // the property cannot be polymorphism and the output is 2.65 Console. writeLine (B. a); 66 Sub s = new Subsub (); 67 // output subsub: gmethod68 s. gmethod (); 69} 70 71}

 

Reprinted please indicate the source: blog Park stonehat http://www.cnblogs.com/stonehat/archive/2012/04/30/2476798.html

 

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.