How Inheritance is useful in reusing code for programmers?

In a large organization, programmers develop a library of classes as they work on various projects. Discuss, in such an environment, how inheritance can be useful in reusing code and therefore saving time.

Related posts:

  1. What is the differences between reusing code and theft of intellectual property?
  2. Five Programming Practices For Organized Code
  3. What Can You Expect From Programmers You Hire?

One Response to “How Inheritance is useful in reusing code for programmers?”

  • parth m:

    Well, with inheritance, several child classes of a given base class will be able to use inheritable methods and fields of the base class as if it were their own, therefore achieving code re usability. They need not be re-programmed in the child classes unless the child class needs different / extra / more constrained functionality from them (this is however applicable to only methods).

    Take a scenario where you, as a programmer have been maintaining a library of classes partaining to geometric shapes for your paint program. Suppose that you have a "Rectangle" class which has four points along with their x,y coordinates, their color values and the code that is used to paint them onto screen. Now suppose that someone else wants to also build a "Square" class, that is one of the most common uses of the Rectangle class. He will build a "Square" class that will inherit from the Rectangle class its four coordinate points and its drawing code. The only difference here is that the Square will impose constraints on the value of the points so that they are equidistant from their adjescent respective points, thus successfully defining a Square. The four coordinate fields as well as the painting code need not be re-programmed in the new Square class. They can now the invoked from a Square instance just as from a Rectangle, provided that were inheritable (public or protected. This will depend on the specific language you use.)

    However, inheritance, solely for code-reuse is strictly discouraged. This is the reason I have deliberately chose a Rectangle and Square example because Square IS-A rectangle, but with some extra properties. Avoid inheritance if such IS-A relationships do not arise.

Leave a Reply