Explain the advantages of inheritance.


Teaching Note:

For example, a parent object holds common data and actions, which enhances reuse and reduces maintenance overheads.


Sample Question:


JSR Notes:

Let's use an example to demonstrate this:

If you are programming for a real estate agent, and have two classes of properties you are working with - apartments, and houses -, but all properties share 15 different attributes, such as price, square meters of floor, address, owner, etc., then why not have one super class of property. And the Apartment sub-class can have all those properties and a couple of its own, such as elevator, and common space. Meantime, the House sub-class can have all Property class, and a couple of its own, such as garden, and shed.

One advantage is that if it comes time to alter the "super class", it only has to be done once, instead of twice in this example, but potentially many times for other examples of inheritance. So there is increased efficiency with maintenance.

And the other thing associated with this, is that when the change is made, it is made consistently for all of the related classes. Whereas if each maintained track of the attribute to be changed, then all but one might get changed correctly if not using inheritance. This means that with inheritance there is increased reliability with maintenance.

One last thing is that uniformity is enforced with things that are indeed, and should be kept, similar.


And a little bit of how the principle of inheritance in general can be a good one:

Take the example of a bunch of models of cars - like w - which use many of the same components.

This is very useful for the same reasons as above:

- If a "parent" component, such as an engine cylinder is adapted, only one has to be, not different cylinders of all of the models.

- If a certain part had to have a defect corrected, then if all models had a different version of them, one either may not be fixed correctly, or may be missed.

- Mechanics the world over will have an easier time working on certain uniform parts of all Skodas.