Object-oriented language Polymorphism

Source: Internet
Author: User

Object-oriented language Polymorphism

In the concepts we have seen for a long time, object-oriented languages have three major features: encapsulation, inheritance, and polymorphism.

Encapsulation:

There is a class concept in the object-oriented language. Therefore, the data and behavior (method) encapsulation is implemented, and the external representation is a unified class or object. Outside the class, you must use the class name or object to access the public members of the category. (Private Members cannot be accessed through classes or objects.

Inheritance:

Class inheritance creates a new class on the basis of the existing class, achieves code reuse, and extends the function mechanism of the existing class. In terms of inheritance, some languages support single inheritance and multi-inheritance (such as C ++), but some languages only support single inheritance (Java language ).

Polymorphism:

Polymorphism is an important feature of programming languages. Many people now have a misunderstanding that only object-oriented languages Support polymorphism. In fact, it is recognized that the first object-oriented language in the world is simula, which was born in 1962. The Functional Programming Language LISP, which was born in 1959, naturally supports polymorphism, but LISP is not an object-oriented language because it does not have the concept of classes.


What Is Object-Oriented polymorphism?

Polymorphism is the same in all languages. C # is no different from other languages.
It is mainly because the subclass inherits the parent class and overrides the method of the parent class to implement different operations.
For example:
Public class parent class {
Public abstract string m ();
}
Public class subclass 1: parent class {
Public override string m (){
Return "1 ";
}
}
Public class subclass 2: parent class {
Public override string m (){
Return "2 ";
}
}

Application:
Parent Class a = new subclass 1 ();
Console. Write (a. m ());
A = new subclass 2 ();
Console. Write (a. m ());

All the above calls A.M (), but the output results are different. This is a polymorphism.

Whether the overload in the object-oriented language is polymorphism

Overload means that the function name can be the same when the function is created, but the row parameters are different. polymorphism refers to the polymorphism caused by the function overload, the specific function with the same name is called based on different row parameters. I do not know if I have clarified it.

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.