2017년 3월 4일 토요일

The succession (programming)

The succession (programming)

The succession (the inheritance: in copter chest which Kei carries) is one of the concepts to constitute object oriented. When a certain object succeeds the characteristic of other objects, it is said that there are "succession relations" among both.

I can be derived in a new class sharing a function, the structure of the existing class by a class-based object-oriented language (a subclass), and it is said, such class "succeeded to a pro-class" (super class) mainly. Specifically, a variable definition or operation (method) are succeeded. In addition, I may succeed only the specifications of the function set like the interface succession of the JAVA.

Generally, when B succeeds to A; B is a A. Semantic relations (Is-a relations) called (B is a kind of A) are managed. Therefore, it is not often appropriate to keep succession relations semantically between unrelated classes because it has the same behavior.

When I do not have the concept called "a class" like a prototype-based object-oriented language (Self, NewtonScript), I call it with "the succession" referring to an object contributing clonal.

The succession and a similar concept have "transfer", but, as for the succession relations decided by the succession once, the transfer object can be changed as needed whereas I am not usually changed.

There are collection (aggregation) and composition collection (composition) as the concept that is different from the succession with Is-a relations in a hierarchy, but the relations between classes are the inclusion relations that are Has-a, and, as for this, the relations between classes are more sparse than the succession.

Table of contents

The significance that I succeed to

Significance succeeding to is quality by the following purposes mainly.

Reuse of the cord

Because structure and the function of the super class are just succeeded by the succession by the subclass, I can reuse the cord of the super class in a subclass.
For example, when there is the function operating of the hush table in a super class, I reuse the function in the subclass and am condition to add the function that I make arbitrary data a hash and store to the top.
I can add the function with abstracting a series of operation, and the re-availability of the cord in this way improves.

The common use by the derivation type and substituted

In the subclass in succession to a certain super class, the use as the model that is equal to the super class is enabled.

By the language of the static model charge account, a variable can store away only the same type of values (or instance), but a variable put out a print in a super class can usually store instance of the subclasses which succeeded to the supermarket class when I use the succession. (there is the exception such as the private succession of the C++, too)
Common use to store away subclass group derived in the sequence of a certain super class together is in this way possible. (but the appropriate down cast is necessary when I treat structure added in a subclass)
And the substitution to construct the derived subclass is possible when I use a function to receive a certain super class with an argument.

In addition, the supermarket class of the origin of derivation comes to be judged with the same thing a derived subclass when I test whether a certain instance is the same as some kind of models.

 class Base{}; class Derivation: public Base{};   int main() {         Base* b = new Derivation(); //OK         return 0, } 

Multiple inheritance and virtual succession

Multiple inheritance means that I succeed from the plural classes. A variation of the multiple inheritance includes the virtual succession. I am divided into two of the imagination succession and the normal multiple inheritance what you would do to the existence of the base class at the beginning when you multiplex plural derivation classes succeeding to from the same class and succeed and make one class.

 class Base { public:     int n; }, The // virtual succession class D1 public virtual Base {/* - */}: class D2 public virtual Base {/* - */}:  class Derivation: public D1, public D2 {/* - */}, 

The state such as this example is called the lozenge succession (the diamond succession) in particular.

Two n called n of basal Base of n and D2 of basal Base of D1 will exist in the case that is not the virtual succession (removes virtual of the part of D1, D2), instance of Derivation particularly (as for the member function, like). When I imagine it and succeed, only as for one, there is merely the part of Base in instance of Derivation. It is in condition that a base of D1 and a base of D2 are shared.

I can use multiple inheritance (implementation multiple inheritance), the virtual succession of the class in the C++ together. However, I can use none in JAVA, C#, D based on C++. I prepare for the succession and more than 0 interface implementation alone instead. The multiple inheritance is because it was thought that there are many problems.

  1. Because succession relations are complicated, overall grasp becomes difficult.
  2. The collision of the name. When plural base classes use the same name in a meaning according to each, it is difficult to perform overriding of both in a derivation class.
  3. Pro-processing implementation is complicated.
  4. When I do not make the virtual succession, plural same base classes exist (the desirable scene has this).
    • This is because what investigate a change becomes serious what of this is a problem when it wanted to do the virtual thing which it did not succeed to for the virtual succession later first. In other words, it means that it is necessary to perform a design properly to use the virtual succession.

However, the intuitive claim has a person using the multiple inheritance and is the situation that it cannot be said that which is right.

Limited multiple inheritance

It is caused by the fact that plural pro-classes are treated concerning structure called the succession equally that complete implementation multiple inheritance is easy to cause a problem. Therefore there is the technique transferring my work to of the complete implementation that instead of succeeding, limited ability. These are called module, トレイト and are distinguished from the normal succession. The difference with the succession is the following thing.

  1. I do not have an instance variable. In other words, I perform a pure method definition not to depend on the specific structure.
  2. I can define set operation of the method
  3. I am added to the class as needed. It is not necessary to succeed them at the time of the normal succession of the class.
  4. It is easy to perform grasp of the structure to have independent succession structure with the normal succession. It is particularly effective in a method defined transversely between the classes which do not have the connection of the meaning.

One or two, 4 can be almost settled among problems of the multiple inheritance from these characteristics. In general, it is thought that it is desirable to do such how to use when I perform multiple inheritance.

Succession in UML

When B succeeds to A in the figure of class of UML, it is said that I have generalized relation to A between B. It may be said that A is the class which generalized B at the same time. On the contrary, it may be said that B is the class which specialized A. For a visual description method, it links B to A in a line and expresses it by describing a triangle of the white space on a colored background in the edge of line on the A side.

Allied item

This article is taken from the Japanese Wikipedia The succession (programming)

This article is distributed by cc-by-sa or GFDL license in accordance with the provisions of Wikipedia.

Wikipedia and Tranpedia does not guarantee the accuracy of this document. See our disclaimer for more information.

In addition, Tranpedia is simply not responsible for any show is only by translating the writings of foreign licenses that are compatible with CC-BY-SA license information.

0 개의 댓글:

댓글 쓰기