4

What is main purpose of inheritance in OOP? i'm new in programming but I think maybe could be code reuse, are there more purposes or a more important purpose?

Joe
  • 173
  • 1
  • 2
  • 9
  • 1
    It is largely used for implementation re-use. Subtype polymorphism itself can be achieved simply with interfaces. Details vary slightly between languages. – user2864740 Sep 11 '15 at 04:58
  • 1
    Yes, the main purpose is code reuse, but it's a complex and inflexible way of doing it. The problem is that your functions and their implementation are tied directly to a class, and so reusing one, or part of one, in particular, requires inheritance to get at them. That is why the age old OOP adage of "prefer composition over inheritance" exists. – jmrah Sep 11 '15 at 10:44

1 Answers1

3

The main purpose of inheritance in Object Orientated Programming (OOP) is to give the user ability to change the behavior of the libraries, without actually changing already working and debugged code.

Changing other people's code always has a risk of introducing bugs because you may not fully understanding how the code works. In OOP, the user inherits the object and implements the behavior changes or new features with his own code, separate from the original code.

This way if there are bugs, the user needs to debug and fix only his own code, which is of course an easier task.

scrowler
  • 24,273
  • 9
  • 60
  • 92
johnfound
  • 6,857
  • 4
  • 31
  • 60