Understanding Inheritance

One of the biggest advantages to using object-oriented coding is inheritance, which is the way one object can inherit behavior and attributes from other objects that are similar to it. When you start creating objects for use in other programs, you will find that some new objects you want are a lot like other objects that already have been developed. For example, if David Lightman does not run afoul of the law because of his ATM scheme, he might want to create an object that can handle error correction and other advanced modem features that weren't around back in 1983 when War Games was released. Lightman could create a new ErrorCorrectionModem object by copying the statements of the Modem object and revising them. However, if most of the behavior and attributes of ErrorCorrectionModem are the same as those of Modem, this is a lot of unnecessary work. It also means that Lightman will have to maintain two separate programs if something needs to be changed or debugged later. Through inheritance, a programmer can create a new class of objects by defining only how it is different from an existing class. Lightman could make ErrorCorrectionModem inherit from Modem, and all he would have to write are the things that make error-correction modems different than previous modems. The way a class of objects inherits from another class is through the extends statement. The following is a skeleton of an ErrorCorrectionModem class that inherits from the Modem class:

class ErrorCorrectionModem extends Modem {
 // program goes here
}


      
Comments