Three basic features of object-oriented programming __ programming

Source: Internet
Author: User
Basic Features

The three basic characteristics of object-oriented are encapsulation , inheritance , polymorphism. Package

The package is best understood. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.
Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding. Inheritance

One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.
New classes created through inheritance are called "subclasses" or "derived classes."
The inherited class is called the base class, the parent class, or the superclass.
The process of inheritance is from the general to the special process.
To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition).
In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.

Inheritance concepts are implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance. Implementing inheritance is the ability to use the properties and methods of a base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but the subclasses must provide the implementation, and visual inheritance is the ability of the subform (class) to use the appearance of the base form (class) and to implement the Code;

When considering using inheritance, it is important to note that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human.

Abstract classes only define generic properties and methods that will be created by subclasses, and when creating abstract classes, use the keyword Interface instead of class.
OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances. polymorphic

1. Concept
Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.

2. Realization Method
To achieve polymorphism, there are two ways, covering, overloading.

3. Overwrite (override)
Overwrite refers to the practice of redefining the virtual function of a parent class by a subclass. It covers a method and rewrites it in order to achieve a different effect. The most familiar overlay for us is the implementation of the interface method, which is generally just a declaration of the method in the interface, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class with the subclass in the inheritance. Be aware of the following points in the overlay:
1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;
2. The return value of the overridden method must be the same as the return of the overridden method;
3. The exception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.

4, heavy Load (overload)
overloading, which means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps the number of parameters is different, perhaps the parameter types are different, perhaps the two are different). It means that we can define some methods with the same name, differentiate them by defining different input parameters, and then, when called, the VM chooses the appropriate method to execute according to the different parameter styles. There are a few things to note when using overloading: You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must differ, for example, can be fun (int,float), but not fun (int,int)); cannot be overloaded with access rights, return types, thrown exceptions ; The exception type and number of methods do not affect overloading; for inheritance, if a method is a priavte in the parent class, it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.

5. Difference
In fact, the concept of overloading is not "object-oriented programming", the implementation of overloading is: The compiler according to the function of a different parameter table, the name of the same name is decorated, and then the same name function is a different function (at least for the compiler). For example, there are two functions of the same name: function func (p:integer): Integer, and function func (p:string): integer; Then the compiler has done a modified function name may be this: Int_func, Str_func. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound (statically bound) at compile time, so overloading and polymorphism are irrelevant. The true and polymorphic correlation is "overlay". When a subclass has redefined the virtual function of the parent class, the parent pointer is dynamic (remember: dynamic) According to the different child-class pointers assigned to it. Call to the function of the subclass, such that the function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given). Therefore, such a function address is bound at run time (dynamic binding).

The conclusion is that overloading is only a linguistic feature, independent of polymorphism and irrelevant to object-oriented. Quote a WORD from Bruce Eckel: "Don't be foolish, if it's not late, it's not polymorphic." ”

6, Function
We know that encapsulation can hide implementation details, make code modular, inheritance can extend existing code modules (classes), and they are all intended to-code reuse. Polymorphism is for another purpose--interface reuse. The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.

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.