Junior — Middle
What is the purpose of inheritance mechanism in object-oriented programming?
sobes.tech AI
Answer from AI
Inheritance mechanism in object-oriented programming is used to create new classes based on existing ones. This allows:
- Reusing the code of the base class without rewriting it.
- Creating class hierarchies that reflect "is-a" relationships.
- Polymorphism — objects of derived classes can be used in place of objects of the base class, which simplifies the extensibility and maintenance of the code.
For example, if there is a base class Animal, you can create derived classes Dog and Cat, which inherit common properties and methods but may have their own specific features.