Polymorphism in Java programming ideology (the third basic feature of object-oriented programming language)

Source: Internet
Author: User
Tags export class

1. In an object-oriented programming language,PolymorphismYes inheritanceData abstractionAndInheritanceThe third basic feature.

2. "encapsulate" a new data type by combining features and behaviors.

3. "implement hiding" by "privatize" the detailsSeparation of interfaces and ImplementationsOpen.

4. The role of polymorphism is to eliminateCouplingLink.

5. Coupling refers to two or more entities.Mutual dependencyIs a measurement of the other party.

6. polymorphism is also called dynamic binding, post-binding, or runtime binding.

7. ChangeMethod callSameMethod subjectAssociation is called "binding ".

8. "pre-binding": binding before the program is executed.Compiler and Connection Program. You do not need to select an object-oriented language.Default.

9. JavaBesides the static and final methods(The Private method belongs to the final method). All other methods are later bound.

10. Why should I declare a method as final?

(1) It can prevent others from overwriting this method.

(2) YesMore important. This can effectively"Disable" dynamic bindingOr, it tells the compiler that it does not need to be dynamically bound. In this way, the compiler can generate more effective code for final method calls. However, in most casesThe overall performance will not change.. Therefore, it is best to decide whether to use final based on the design, instead of using final for the purpose of trying to improve performance.

11. A simple example of polymorphism:

class Shape{   void draw(){      System.out.println("shape draw()");   }   void erase(){      System.out.println("shape erase()");   }}class Circle extends Shape{   void draw(){      System.out.println("Circle draw()");   }   void erase(){      System.out.println("Circle erase()");   }}class Square extends Shape{   void draw(){      System.out.println("Square draw()");   }   void erase(){      System.out.println("Square erase()");   }}public class Shapes{   public static void main(String[] args){      Shape shape = new Circle();      shape.draw();      Shape shape1 = new Square();      shape1.draw();   }}

Running result:

Due to polymorphism (later binding), circle draw () and square draw () are correctly called ().

12. The "abstract Method" in Java is equivalent to the pure virtual function in C ++.

13. constructors do not have multi-state rows (they are actuallyStatic Method, But the static statement is implicit ).

14. If a dynamic binding method is called within the constructor, the member manipulated by the called method may not be initialized (It may cause some difficult-to-discover hidden errors.). Example:

Class glyph {void draw () {system. out. println ("glyph (). draw () ") ;}; glyph () {system. out. println ("glyph () before draw ()"); Draw (); // due to dynamic binding, the draw () system of roundglyph is called. out. println ("glyph () after draw ()") ;}} class roundglyph extends glyph {private int radius = 1; roundglyph (int r) {radius = r; system. out. println ("roundglyph. roundglyph () radius = "+ radius);} void draw () {// note that the following output is roundglyph. draw () radius = 0 system. out. println ("roundglyph. draw () radius = "+ radius) ;}} public class polyconstructors {public static void main (string [] ARGs) {New roundglyph (5 );}}

Running result:

15. Initialization of complex objects:

1) initialize the bucket allocated to the object to be binary before any other thing occurs.Zero.

2) call the base class constructor, a base class that repeats recursion until the lowest layer.

3) Call the initialization method of the member in the declared order.

4) Call the constructor of the export class

16. If it is not later bound, it is not a polymorphism. For exampleMethod overloadIs a non-object-oriented feature.

The above content is compiled from Java programming ideas. If any omission exists, please leave it blank!


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.