2.2.2 Inheritance |
Inheritance allows an object to inherit the methods and attributes of an other object. The object that is inherited from is called the ancestor or super-class. The object that inherits the methods and attributes is called the descendent or sub-class.
In the banking system a object called Account could be created. The Account object would have the account number and balance attributes and it would have the depoist and withdraw methods. Then a more specilized object SavingsAccount could be created, inherting from Account. This means that the SavingsAccount object would have all the attributes and methods of the Account object plus any new attributes and methods that have been defined for the SavingsAccount object. Similary another object, CheckingAccount could also inherit from the Account object in the same way.
As well as inheirting attributes and methods, a descendent object can override an inherited method and replace it with one of it's own. For example the Account object could have a method to calculate the fees on the account. The SavingsAccount object could override this method and replace it with one of it's own that calculated the fees for the SavingsAccount object in a different way.