| CS 1371 Dept. Presents: |
| INHERITANCE | |
|---|---|
| Definition | In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures such as queues, stacks, collections, windows, menu boxes the structure of different object families can be viewed as a kind of "family" tree.
Most OO languages allow us to use these types of relationships to reuse code and functionality by making classes that use characteristics of "parent" classes Properties of Inheritance: Classes often share capabilities. We want to avoid re-coding these capabilities. Reuse of these would be best to: - Improve maintainability, - Reduce cost, - Improve “real world” modeling. When one class re-uses the capabilities defined in another class.
The new subclass gains all the methods and attributes of the superclass.
Benefits of Inheritance: Two Types of Inheritance: |
| Example |
function
acct = SavingsAccount(data) ---> Name of the class (matches the file
and directory name)
if nargin == 0
parent = BankAccount;
acct.RATE = 5;
acct.MIN_BALANCE = 1000;
acct = class(acct,'SavingsAccount', parent);
elseif
isa(data,
'SavingsAccount')
acct = data;
else
parent = BankAccound(data);
acct.RATE = 5;
acct.MIN_BALANCE = 1000;
acct = class(acct,'SavingsAccount', parent);
end
|