Submit Article


OOP principles. Inheritance


Inheritance - one of the four major mechanisms of object-oriented programming (along with encapsulation, polymorphism and abstraction), which allows to describe a new class based on an existing (parent), with the features and functionality of the parent class borrowed a new class.

In other words, the subclass implements the specification of an existing class (base class). This allows you to objects derived class exactly the same way as with the objects of the base class.

Types of inheritance. Simple inheritance

The class, after which the inheritance is called the base or parent (born base class). Classes that have occurred from the base, called descendants, heirs or derived classes.

In some languages, the abstract classes. Abstract class - a class that contains at least one abstract method, it is described in the program, has fields, methods, and can not be used to directly create the object. That is, from the abstract class can only inherit. Objects are created only on the basis of derived classes, inherited from the abstract. For example, an abstract class can be a base class "employee of the university", which inherits from "student", "professor", etc. Since the derived classes have common fields and functions (eg, field "birth year"), then these members of the class can be described in the base class. The program creates objects of class-based "student", "professor", but it makes no sense to create an object based on the class "employee of the university".

Multiple inheritance

With multiple inheritance a class may be more than one ancestor. In this case, the class inherits the methods of their ancestors. The advantages of such an approach is more flexible. Multiple inheritance is implemented in C + +. Of the other languages, providing this opportunity, it may be noted Python and Eiffel. Multiple inheritance is supported in the language UML.

Multiple inheritance - a potential source of errors that may arise due to the presence of identical method names in the ancestors. In languages that are positioned as the heirs of C++ (Java, C# etc.), multiple inheritance was dropped in favor of interfaces. Almost always you can do without the use of this mechanism. However, if such a need yet arisen, for conflict resolution using inherited methods with the same name may, for example, apply operation increase visibility - "::" - to invoke a particular method of the parent.

An attempt to solve the problem of availability of the same method names in the ancestors had been made in the language Eiffel, in which the description of a new class must be explicitly imported by members of each of the inherited classes and naming in a child class.

Most of the modern object-oriented programming languages (C#, Java, Delphi, etc.) supports the ability to simultaneously derive from ancestor class methods and implement multiple interfaces in the same class. This mechanism allows the largely replace multiple inheritance - interface methods must explicitly override that eliminates errors in the succession to the functionality of the same methods of different classes of ancestors.

Inheritance (programming)

Inheritance - one of the four major mechanisms of object-oriented programming (along with encapsulation, polymorphism and abstraction), which allows to describe a new class based on an existing (parent), with the features and functionality of the parent class borrowed a new class.

In other words, the subclass implements the specification of an existing class (base class). This allows you to objects derived class exactly the same way as with the objects of the base class.

Types of inheritance

Simple inheritance

The class, after which the inheritance is called the base or parent (born base class). Classes that have occurred from the base, called descendants, heirs or derived classes (English derived class).

In some languages, the abstract classes. Abstract class - a class that contains at least one abstract method, it is described in the program, has fields, methods, and can not be used to directly create the object. That is, from the abstract class can only inherit. Objects are created only on the basis of derived classes, inherited from the abstract. For example, an abstract class can be a base class "employee of the university", which inherits from "student", "professor", etc. Since the derived classes have common fields and functions (eg, field "birth year"), then these members of the class can be described in the base class. The program creates objects of class-based "student", "professor", but it makes no sense to create an object based on the class "employee of the university".

Multiple inheritance

With multiple inheritance a class may be more than one ancestor. In this case, the class inherits the methods of their ancestors. The advantages of such an approach is more flexible. Multiple inheritance is implemented in C + +. Of the other languages, providing this opportunity, it may be noted Python and Eiffel. Multiple inheritance is supported in the language UML.

Multiple inheritance - a potential source of errors that may arise due to the presence of identical method names in the ancestors. In languages that are positioned as the heirs of C + + (Java, C # etc.), multiple inheritance was dropped in favor of interfaces. Almost always you can do without the use of this mechanism. However, if such a need yet arisen, for conflict resolution using inherited methods with the same name may, for example, apply operation increase visibility - "::" - to invoke a particular method of the parent.

An attempt to solve the problem of availability of the same method names in the ancestors had been made in the language Eiffel, in which the description of a new class must be explicitly imported by members of each of the inherited classes and naming in a child class.

Most of the modern object-oriented programming languages (C #, Java, Delphi, etc.) supports the ability to simultaneously derive from ancestor class methods and implement multiple interfaces in the same class. This mechanism allows the largely replace multiple inheritance - interface methods must explicitly override that eliminates errors in the succession to the functionality of the same methods of different classes of ancestors.