Logout

D.2.5

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 Questions:


----

Q1. Outline two ways in which the programming team can benefit from the way the relationships between the three objects, XyzSuperClass, AbcSubClass and DefSubclass, have been represented in the code.

----

Q2. The team has decided to use inheritance in its design.
Describe one benefit that inheritance brings to OOP.

----

Q3. The company that owns this restaurant (classes provided in the exam paper) also owns hotels, shops, and a car hire business. The programs that manage the finances of these different businesses include classes similar to the ones shown in this paper.

Explain how inheritance could be used as a tool to both improve the clarity of the design and to reduce the amount of code that needs to be written.

----



sdfsdfsf

 

JSR Notes:

Let's start with an example:

You are programming for a real estate agent, and have several classes of buildings/properties you are working with - including apartments and houses, for example. And all classes of buildings/properties share 15 different attributes, such as price, square meters of floor, address, owner, etc. So why not have one super class of property?

The Apartment sub-class, for example, can have all those 15 common attributes plus a couple of its own, such as hasElevator, and commonSpaceSize. Meantime, a House sub-class could have all Property class attributes, plus hasGarden, and hasShed. This arrangement will yield several advantages, mainly through the avoidance of duplication, but also because the program's structure will model the real situation.


Advantages of Inheritance

Avoidance of Duplication (and so...)

- Faster Development

During initial development, but also when it comes time to alter any of the attributes or the methods that work with them, that work will only have to be done once, instead many times. This increases overall speed efficiency, both with development and maintenance.

- Increased Reliability

Avoidance of duplication also leads to consistently within all related classes. With the alternative approach multiple classes all containing a certain common attribute or methods would each need to be changed. This increases the possibility of errors occurring, or even without obvious errors, inconsistencies creeping in, which may cause problems down the line. All of this is completely avoidable with the inheritance approach.

- Simplified Testing

One super-class means one round of testing, rather than having to test each class that had a duplicate attribute and related methods. Not only is this simpler, it's faster.


Modeling of Real-world Relationships

The other major advantage to inheritance is the way it models real-world relationships. Recall how the grouping of related attributes and the methods that act upon them, in encapsulation, can better model real world situations. In the same sort of way, modeling the macro level a complex system in a hierarchical way that reflects their real world relationships can make the structure of the program easier to understand. And this in turn makes it less prone to errors.

 

 

In Summary, Back to the Assessment Statement:
Explain the advantages of inheritance.

 

And for specific scenarios in exam questions:

A generic Xyz superclass can be created which would contain attributes/methods required by all subclasses. Each of the different subclasses could then inherit all these tributes and methods, and add new attributes/methods that relate only to them. (And all of this yields the advantages listed above...)