Chapter 10. IMPROVEMENT THROUGH INHERITANCE

Anyone who has played Monopoly knows that a player's financial situation can be improved through inheritance. In object-oriented programming (OOP), inheritance can also bring improvement. In this chapter we'll use inheritance to improve the MySQL classes developed in Chapter 9, by simplifying error trapping and by modifying the behavior of the MySQLResultSet class.

Trapping errors is not a job that developers approach with enthusiasm. It is tedious and a distraction from the task at hand. No one sets out to write error-handling code; it is a necessary evil. Not only that, error trapping is ugly. It clutters up well-written code and often ends up obscuring what was initially readable. Further, it's not a good idea to write error trapping in the early stages of development because you want errors to be readily apparent. For these reasons error trapping is often left until the final stages of development and, if it is done at all, it is tacked on more or less as an afterthought.

One of the big advantages of OOP is the ability to catch exceptions rather than trap errors. By catching exceptions, the task of handling errors can be centralized. This makes for much tidier code and eases the transition from development to production code—erasing the need to tack on error trapping at the end.

This improvement to error handling is made possible because of a built-in class, Exception. In this chapter you will use this class as the base class for building your own exception class.

The second improvement we'll apply involves modifications to the user-defined class, MySQLResultSet. As you saw in the previous chapter, result sets and arrays have characteristics in common; you often need to iterate through them to examine each element. It is exceptionally easy to traverse an array by using a foreach loop. This easy traversal is the modification in behavior that we have in mind for the MySQLResultSet class. In this case, a built-in interface (rather than a class) facilitates adding this behavior to the MySQLResultSet class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.113.193