1

In object oriented programming class the instructor is telling that

Inheritance is called Object code level reuse.

But I was thinking how because We don't have the source code is not available in object or any other compiled library file so how can we inherit a function or class for reuse if We don't have the source code.

Normally we inherit a class whose source code or class information is available.

Can somebody explain it?

The other question need the explanation is :

how container-ship is different than inheritance?

Because I read that container-ship is a derive which has a relation-ship from base class as "has a relation", which I feel is inheritance only. Any explanation is appreciated.
Thanks.

Amarjeet Sharma
  • 168
  • 1
  • 11
  • http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance http://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition http://www.javaworld.com/article/2076814/core-java/inheritance-versus-composition--which-one-should-you-choose-.html – Azodious Dec 05 '16 at 06:31
  • Inheritance is the ability for a class to inherit properties and behavior from a parent class by extending it, while Containership is the ability of a class to contain objects of different classes as member data. Inheritance represents a "is-a" relationship, while Containership represents a "has-a" relationship. iI a class is contained in another, the container does not get the ability to change or add behavior. – CompSciFly Dec 05 '16 at 06:32
  • Even if somebody else wrote it, you are still saving time by using the base classes, so you *are* reusing code even if you don't have access to the source code. On the other hand, you can reuse existing code by copy-pasting it around the codebase, which would work in a very different way. – vgru Dec 05 '16 at 06:39
  • On the other hand there are many *other* ways of reusing object code. If your instructor is really saying 'Inheritance is called Object code level reuse', which I doubt frankly, he is wrong. It isn't 'called' that, but it is an *example* of it. – user207421 Dec 05 '16 at 06:55
  • @EJP but how can we inherit something for which source code info is not there because object code will be in non readable form. Or the statement is incomplete without the info about the object properties and methods details. – Amarjeet Sharma Dec 05 '16 at 07:54
  • @AmarjeetSharma How depends on the language. C++ doesn't need anything more than the class *declaration* for inheritance, i.e. the `.hpp` file. Java only needs the .class file. Inheritance wouldn't be much use otherwise. – user207421 Dec 05 '16 at 07:57

0 Answers0