What is code reuse How does it relate to inheritance?

What is code reuse How does it relate to inheritance?

Inheritance in object-oriented programming languages supports reuse of existing code within new applications. It does this by allowing a developer to define new code in terms of existing code and new extensions. This is a different kind of code reuse from that found in non-object-oriented languages.

Which is used to reuse a code when there is no inheritance?

Don’t use inheritance just to get code reuse If all you really want is to reuse code and there is no is-a relationship in sight, use composition. Don’t use inheritance just to get at polymorphism If all you really want is a polymorphism, but there is no natural is-a relationship, use composition with interfaces.

What do you mean by reusability?

In computer science and software engineering, reusability is the use of existing assets in some form within the software product development process; these assets are products and by-products of the software development life cycle and include code, software components, test suites, designs and documentation.

What are the examples of reuse?

The following are some examples of reuse.

  • Containers can be reused at home or for school projects.
  • Reuse wrapping paper, plastic bags, boxes, and lumber.
  • Give outgrown clothing to friends or charity.
  • Buy beverages in returnable containers.

What is meant by ” inheritance for external reuse “?

By “inheritance for external [code] reuse” they mean cases where a subsystem exposes (makes publicly available) an abstract base class, which is to be extended by other classes either of this subsystem or of other subsystems. So, in essence, code that uses the base class has knowledge of the fact that inheritance is utilized.

What is meant by reuse of inheritance in object oriented?

By “inheritance for internal [code] reuse” they mean cases where a subsystem exposes (makes publicly available) an interface, which is internally implemented by a number of classes that are not publicly exposed, and in order to achieve code reuse the subsystem makes use of an also package-private common base class for all the implementing classes.

Why is code reuse via inheritance not a problem?

To give a slightly different viewpoint: Code-reuse through inheritance is not a problem if private inheritance was used, because then the Liskov substiturion principle does not apply. This assumes of course that the language in question supports private inheritance.

When do you use inheritance in a program?

If a team is working on a big program or piece of code, then inheritance acts like a blessing. We need not write code repeatedly; if there is a hierarchy in classes or classes that have a dependency on one another, we can use inherit code from base class to child class. This will prevent from the writing of the same code repeatedly.