The Trick Of The Mind - The Benefit Of Objects |
Written by Mike James | ||||||
Wednesday, 01 October 2025 | ||||||
Page 2 of 2
InheritanceSo far most people would say that objects are a great idea – they provide a way to package subroutines with data and they offer many benefits with few downsides. However, this is not the case with the object-oriented method’s biggest proclaimed advantage, code reuse via inheritance. Suppose you have an implementation of a Person object complete with all the data fields and methods that are required. Now suppose that you want to add a Customer object to your program – how can you do this? You could start from scratch and write the instructions for your new object, but you already have a Person object and surely a Customer is a Person? There should be some way of using the Person object to create a Customer object. The most direct way, and the one in use since almost the start of programming, is copy-and-paste reuse. You simply take the instructions that make up the Person object and copy and paste them to a new part of the program and make the changes and additions needed to create the Customer object, maybe by adding a field for a customer number. Copy-and-paste reuse is simple, but it actually copies instructions and if you find a bug in Person you have to remember to correct it in Customer and any other objects that you derived from Person. This leads to the belief that there should be a better way. Currently that better way is generally accepted to be “inheritance”. You can create a new class and specify that it should inherit everything that another class has. The class that inherits is called a “derived” or “child” class and the inherited class is the “base” or “parent” class. The derived class has all of the properties and methods of the base class. If this was all there was to inheritance it would be useless because the derived class would be identical to the base class. What makes it useful is that, like copy-and-paste reuse, you can add properties and methods to the derived class. What this means is that you can extend the derived class to create new behavior. For example, the Customer class derived from Person can have a customer number field added together with any methods that work with that field that are needed. Extending derived classes in this way isn’t controversial and it isn’t very dangerous. If you find a bug in the Person class then you can correct it and expect any derived class to be updated to be correct without you having to do anything extra about it. Inheritance is dynamic and changes to the base class are automatically passed on to the derived classes. This is good, but there is a risk that the new methods in the derived class will be broken by changes to the way the base class works. For example, suppose the Customer class constructed a customer number using the Age field in the Person class and the way that this was recorded was changed from a number to a string. This change might not cause any problems for the base class because it doesn’t do anything with the Age field, but the derived class would surely be broken by the change. To stop this happening the encapsulation principle is extended from just the external world to the derived class. That is, as far as the derived class is concerned the base class is a black box and it cannot access its internal workings. This is easy to say, but very difficult to implement. Different languages provide different ways of isolating the internals of the base class from the derived class. The stronger the isolation the less useful the base class is to the derived class as derived classes usually want to access the internal workings to extend what the base class does! OverridingThings get even worse when you allow the derived class to modify what the base class does. Most languages allow the programmer to redefine anything inherited from the base class. Usually this involves writing new versions of inherited methods called “overriding” methods. The inherited methods that are replaced by the overriding methods are said to be overridden. The overriding methods are used in the derived class in place of the inherited, overridden method – the overriding method replaces the inherited method. If an overridden method is corrected in the base class then it is clear that this change isn’t going to be passed on to the derived class. Thus, overridden methods break the connection to the base class. What is worse, the overriding method may do things in a completely different way to the base class and this may make any changes to the base class less compatible with the derived class. Put simply, overriding inherited methods isn’t a safe thing to do and different language impose restrictions and mechanisms to make it safer. In many you can’t override an inherited method unless you state clearly that this is what you are doing. In chapter but not in this extract
Summary
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
||||||
Last Updated ( Wednesday, 01 October 2025 ) |