18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10 Inheritance<br />

This chapter <strong>in</strong>troduces the concept of <strong>in</strong>heritance <strong>in</strong> which an exist<strong>in</strong>g class can be specialized without<br />

modify<strong>in</strong>g the orig<strong>in</strong>al class. By us<strong>in</strong>g this technique software re-use can become a practical consideration<br />

when develop<strong>in</strong>g software. Thus a programmer can become a builder of software us<strong>in</strong>g previously<br />

developed components.<br />

10.1 Introduction<br />

Inheritance is the ability to create a new class by us<strong>in</strong>g the methods and <strong>in</strong>stance attributes from an exist<strong>in</strong>g class<br />

<strong>in</strong> the creation of a new class. For example, a class Account that provides the methods deposit, withdraw,<br />

balance, and statement can be used as the base for a new class that provides the ability to pay <strong>in</strong>terest on the<br />

outstand<strong>in</strong>g balance <strong>in</strong> the account. The new class Interest_Account <strong>in</strong>herits all the methods and <strong>in</strong>stance<br />

attributes from the class Account and adds to these the new methods of Calc_Interest, Add_Interest,<br />

and Set_Rate plus the <strong>in</strong>stance attribute Accumulated_Interest. This is illustrated <strong>in</strong> Figure 10.1.<br />

Class Account Class Interest_Account Actual components of<br />

Class Interest_Account<br />

Account<br />

Deposit<br />

Withdraw<br />

Balance<br />

Balance_Of<br />

Interest_Account<br />

Add_Interest<br />

Calc_Interest<br />

Set_Rate<br />

Accumulated_Interest<br />

Interest_Account<br />

Deposit<br />

Withdraw<br />

Balance_Of<br />

Add_Interest<br />

Calc_Interest<br />

Set_Rate<br />

Balance_Of<br />

Accumulated_Interest<br />

Figure 10.1 components of Account and the derived class Interest_account.<br />

Note:<br />

The class Interest_Account has the same visibility of components <strong>in</strong> the base class Account as<br />

would a client of the class. In particular, it has no access to the private <strong>in</strong>stance attributes of the base<br />

class. Thus methods <strong>in</strong> the class Interest_Account cannot access the base class <strong>in</strong>stance<br />

attribute Balance_Of.<br />

However, for a class type to be fully extended it must be declared as tagged. If a class type is not tagged<br />

then it can only be extended by add<strong>in</strong>g new methods. New <strong>in</strong>stance attributes may not be added.<br />

The consequence of this is that an implementor of a class must explicitly declare the class record type<br />

tagged if new classes are to be derived from it.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!