Inheritance
wikipedia Inheritance (object-oriented programming)
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation.
NOTE: 两种流派的OOP中都支持inheritance。
Also defined as deriving(派生) new classes (sub classes) from existing ones such as super class or base class and then forming them into a hierarchy of classes.
In most class-based object-oriented languages, an object created through inheritance, a "child object", acquires(获得) all the properties and behaviors of the "parent object" , with the exception of: constructors, destructor, overloaded operators and friend functions of the base class.
Inheritance allows programmers to create classes that are built upon existing classes,[1] to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a directed graph.
Inheritance was invented in 1969 for Simula[2] and is now used throughout many object-oriented programming languages such as Java, C++ or Python.
An inherited class is called a subclass of its parent class or super class. The term "inheritance" is loosely used for both class-based and prototype-based programming, but in narrow use the term is reserved for class-based programming (one class inherits from another), with the corresponding technique in prototype-based programming being instead called delegation (one object delegates to another).
NOTE: inheritance and delegation:
是可以通过delegation来实现inheritance类似的效果的,参见
Theory\Programming-paradigm\Object-oriented-programming\Assemble\Composition
章节。
Inheritance VS subtyping
Inheritance should not be confused with subtyping.[3][4] In some languages inheritance and subtyping agree,[a] whereas in others they differ; in general, subtyping establishes an is-a relationship, whereas inheritance only reuses implementation and establishes a syntactic(语法) relationship, not necessarily a semantic relationship (inheritance does not ensure behavioral subtyping). To distinguish these concepts, subtyping is also known as interface inheritance, whereas inheritance as defined here is known as implementation inheritance or code inheritance.[5] Still, inheritance is a commonly used mechanism for establishing subtype relationships.[6]
NOTE: 上面这一段所描述的是inheritance VS subtyping,在
Theory\Programming-paradigm\Object-oriented-programming\Subtyping-polymorphism\Subtyping-VS-inheritance
章节中,对这个话题进行了专门的描述。
Inheritance VS composition
Inheritance is contrasted with object composition, where one object contains another object (or objects of one class contain objects of another class); see composition over inheritance. Composition implements a has-a relationship, in contrast to the is-a relationship of subtyping.
NOTE: 关于 composition over inheritance,参见
Theory\Programming-paradigm\Object-oriented-programming\Design-pattern\Principle\Composition-over-inheritance
章节。