The three cornerstones of object-oriented (encapsulation, inheritance and composition, polymorphism)

Source: Internet
Author: User

Turn from the three cornerstones of object-oriented object-oriented three cornerstones (encapsulation, inheritance and composition, polymorphism)

One of the three cornerstones of the package

1. What is encapsulation?

Encapsulation (encapsulation) is also known as the hidden implementation (hiding the implementation). is to expose only the external interface of the code unit, and hide its specific implementation.

For example, your mobile phone, keyboard, screen, handset, etc., is its external interface. You just need to know how to use the phone with a button, without needing to know how the inside of the phone works. The encapsulation mechanism exposes the external interface just like a mobile phone, without requiring the user to understand its internal implementation. Careful observation, the reality of a lot of things have such characteristics.

2. How to implement encapsulation?

In programming, encapsulation is often achieved through access control. C++,JAVA,AS3 have public, Protected, Private, and other access control characters. by exposing information to public, private,protected information is hidden to achieve encapsulation.

a good OOP programmer will try not to expose the code to the public, which is like using the Private keyword . Because in OOP, the tighter you control your code access, the more freedom you will have to modify your code later.

--excerpt from the road of the AS3 Palace. This is explained in more detail below.

3. Why encapsulation? The benefits of encapsulation.

A. Encapsulation makes it more secure and easy to modify the code. The code is divided into relatively separate units.

As long as the external interface of the phone (keyboard, screen, usage, etc.) does not change, you do not need to re-learn to use the next generation of phones, regardless of the internal circuitry of the phone and how the technology is improved. Similarly, as long as the car's steering wheel, brakes and other external interfaces do not change, then, no matter how to transform its engine, you will also drive this kind of car.

The benefit of encapsulation is that it clearly indicates that those properties and methods are externally accessible. So when you need to adjust the code for this class, just make sure the public property is the same, the parameters and the return value type of the public method are the same, then you can modify the class without affecting the rest of the program, or other programs that use the class. That's why you just said "in Oop, the more you control code access, the more freedom you will have to modify your code later."

B. Encapsulation greatly reduces the overall complexity of software development.

You can use other people's classes very well without worrying about how their internal logic is implemented. You can easily learn to use code written by someone else, which makes it much less difficult to develop software collaboratively.

C. Encapsulation also avoids the problem of naming conflicts.

Encapsulation has an isolation effect. The buttons on the phone and the buttons on the TV remote are certainly of different use. But they can all be called keys, why don't you get mixed up? Apparently one belongs to the phone class one belongs to the remote control class. Different classes can have methods and properties of the same name, but are not confused.

The two inheritance and recombination of three major cornerstones

The core idea is not just to reuse existing code, but to create new classes with some of the classes already in place.

1. Inheritance

inheritance, inheritance, is a seemingly magical way of reusing code.

In addition to this, inheritance (which is limited to public inheritance , private inheritance, and the protection of inheritance will break the feature of upward conversion) also produces another wonderful thing called up-conversion or up-mapping (upcasting). This means that the subclass object can be used as a parent object in code.

In fact, the inheritance thought mentioned here is not common in human life (the succession in program design is also more difficult to master), more than the present human classification of nature. We observe the world around us and find that people prefer to use composite and a special form of inheritance (inheritance of abstract classes, which is described in polymorphism.) such as automobiles inherit from the abstract class of vehicles).

2. Composite

Compound, composition, the various parts are grouped together. Programming is to use existing classes of objects to generate new classes.

The table is made up of planks and nails, and the lamp uses a lamp holder, a lamp, a wire, a joint, etc. We find that many things around us are made up of smaller things, like bricks. I believe that when you were young, you also opened up a lot of things you are curious about, to see exactly. Go and see if this new class is made up of those other classes. In fact, when you are very young, you have understood the compound.

In the program design, the composite embodiment in the generated new class uses the existing class instance, the specific code example here also does not give, I believe you can understand at a glance.

Compounding makes it easier and more intuitive to generate new classes, and very easy to implement, and most people (including in real life) prefer to compound, compared to inheriting this method of constructing new classes through existing classes.

3. When to use inheritance, when to use composite?

In the actual programming, the use of composite frequency is far more than inheritance, for OOP novice, to be cautious with inheritance, use complex. Some extreme oop advocates compound hatred of inheritance, claiming that inheritance is "evil" and that everything is compounded. Although it is extreme, broad home a smile, but the actual application of composite is more flexible than inheritance, and more straightforward.

While inheriting one of the three cornerstones of object-oriented, incorrect use of inheritance is not just a disaster for code maintenance, but also a logical twist. Therefore, after reading the following paragraph, you still do not understand when to use the composite when the inheritance, please consider the composition.

A. Consider inheritance when you need to use an upward conversion.

B. Use "has a" and "is a" to differentiate between composition and inheritance. That is, consider a new class that has an object (with a) existing class (such as a new class for automobiles, an existing class for tires, consider a composite), or a new class is a special case of an existing class (is a) (such as butterflies and insects, considering inheritance).

Tri-polymorphism of three major cornerstones

Polymorphism, polymorphism, means a variety of forms.

The generalized polymorphism consists of the following examples:

A. The home fuse is broken, you use wire instead, this implements the forced type conversion, forcing the use of wire as a fuse. This is called forced polymorphism. The embodiment of the code is forced type conversion, like the use of wire to replace the fuse may be dangerous, forced type conversion may also result in loss of precision and other consequences.

B. Another polymorphic called overloaded polymorphic , operator overloading and function (method) overloading is overloaded polymorphism.

C. There are also parameter polymorphism , in which the performance of the code is class template and function template.

d. Below, the emphasis is on the inclusion of polymorphism. The following explains the abstract classes and interfaces.

Abstract class

people combine a function of something together to form an abstract class. Transport shows that this abstract class of sub-class cars, trains, etc. have the ability to transport people or goods. Drugs have a therapeutic effect. Can be used to drink tea is called tea set ...

interface (Interface)

Besides interfaces, Interface, interfaces contain only a set of method declarations without a specific code implementation. As long as the class that implements the same interface has the characteristics of this interface. An interface is like a protocol that describes an external commitment to an object that implements an interface. This allows other objects to communicate with the object that implements the interface according to this protocol.

In fact, the abstract class realizes the function of the interface , if you do not understand the interface temporarily, you can understand the abstract class as an interface.

Why use abstract classes or interfaces?

An abstract class acts as a parent class in conjunction with some of the child classes. It is important to exist as a common type of subclass when used, while giving the subclass maximum flexibility.

An important rule in OOP programming is the dependency reversal principle (dependence inversion Principle), which means that to do any specific code implementation, the first step is to rely on the implementation of the abstract class. This principle is expressed in many ways, and one of the well-known explanations is that programming is based on the interface, not on the implementation. use abstract classes as much as possible in specific code, rather than using specific classes (which is, of course, an idealized realm).

Here's a code example, which is actually an example in the fundamentals of C + + object-oriented programming. There is an abstract class shape, with draw and erase methods, and of course, draw and erase are empty methods, implemented in C + + with pure virtual functions. The round class inherits from the shape class, overriding the draw and erase methods of shape to draw circles and clear the shapes. The line class inherits from the shape class, which overrides shape's draw and erase methods to draw straight lines and clear graphics. The Triangle class inherits from the shape class and overrides the draw and erase methods of shape to draw triangles and clear the shape. Their UML representations are:

So we can write a drawshape function like this:

void Drawshape (shape& s) {  s.draw ();}

Note that the parameter type of this function is shape, which means that it is possible to place an instance of Round, line, or Triangle class, when:

      Round R;      Drawshape (R);

That is, you can draw a circle when:

       Line L;       

The drawing is a line. Now you can probably see the flexibility that abstract classes (interfaces) bring to your code. And we can add subclasses to the shape parent class at any time.

The three cornerstones of object-oriented (encapsulation, inheritance and composition, polymorphism)

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.