MATLAB Documentation for OOP
(For reference by students)


 

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:
- Saves effort of “reinventing the wheel.”
- Allows us to build on existing code, specializing without having to copy it, rewrite it, etc.
- To create the subclass, we need to program only the differences between the superclass and the subclass that inherits from it.
- Allows for flexibility in class definitions.

Two Types of Inheritance:
1.Extension
- Adds attributes
- Adds methods

2. Redefinition (Method Overriding)
- Changes/modifies existing methods, specializing as needed

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
 
 


Got comments\feedback\suggestions\complaints\anything whatsoever? Shoot the webmaster an email!

Website created & maintained by Shardul Bhatia and Wen Eu Cheah