Tell me please, is it considered the norm in programming to inherit by the set principle -> the set element? For example, I have a class "ring of polynomials with integer coefficients," and I want to inherit from it the class of "polynomial with integer coefficients."
2 answers
Yes, it is considered the norm. This is the essence of inheritance - the heir class implements the specification of the parent class. I would even make the class "ring of polynomials with integer coefficients" abstract and have already inherited all special cases from it.
It is worth being careful with such inheritance, because an element of a set can be an instance of a class representing a set, and not a class derived from it.
For example, a class that represents people with a specific set of properties and an instance is a specific person. Such a person is unique and it is hardly necessary to inherit something from him.
In your example, you can consider a polynomial as a derived class of a polynomial ring , although I myself would use (and would recommend to you) a class representing a ring of polynomials with some container containing these polynomials and an operation of adding a new polynomial to the set representing the ring, with appropriate verification operations a new polynomial on the ring belonging (there are 6 of them, as far as I remember). Here, however, it is considered that you have a finite set of polynomials.
In the case of using inheritance, remember the following:
You need to decide on the type of inheritance, i.e. whether it is realized by means or is a variety .
Consider alternatives to inheritance, for example, using composition.
In your example, multiple inheritance can be used for a polynomial , i.e. not only from the ring, but from another class.