Understanding Inheritance

A big advantage to OOP is inheritance, which enables one object to inherit behavior and attributes from another object.

When you start creating objects, you sometimes find that a new object you want is a lot like an object you already have.

What if David Lightman wanted an object that could handle error correction and other advanced modem features that weren’t around in 1983 when WarGames 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 would have two separate programs to update if something needed to be changed later.

Through inheritance, a programmer can create a new class of objects by defining how they are different than an existing class. Lightman could make ErrorCorrectionModem inherit from Modem, and all he would need to write are things that make error-correction modems different than modems.

A class of objects inherits from another class by using the extends statement. The following is a skeleton of an ErrorCorrectionModem class that inherits from the Modem class:

public class ErrorCorrectionModem extends Modem {
    // program goes here
}

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

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