MATLAB Answers

1

Can multiple classes share external methods without inheritance?

Asked by Justin Brantley on 16 Aug 2019
Latest activity Commented on by Justin Brantley on 19 Aug 2019
I am trying to explore OOP capabilities in MATLAB for a project I am working on. The idea is that I want to have a project class and within that, subclasses for each data type. For example:
classdef myproject < handle
properties (SetAccess = public, GetAccess = public)
d1
d2
d3
end
methods (Access = public) % constructor
function self = myproject
d1 = data_class1;
d2 = data_class2;
d3 = data_class3;
end
end % end constructor
% Public methods
methods (Access = public)
function myproject_filter(self)
% external filter here
% can
end
end % end public methods
end % end myproject class
%%%%%%%%%%%%%%%%% NEW CLASS %%%%%%%%%%%%%%%%
classdef data_class1 < handle %?? - not a subclass, right?
properties (SetAccess = public, GetAccess = public)
data
samplingrate
end
methods (Access = public) % constructor
function self = myproject
self.data = load("data_class1_data.mat") % some file
self.samplingrate = 1e500000; %Hz - absurd sampling rate
end
end % end constructor
% Public methods
methods (Access = public)
% generic method
function myproject_filter(self,filt_band)
% external filter
localdata = external_filter(self.d1.data,.self.samplingrate,filt_band);
self.data = localdata;
end
% class specific method
function dataclass1_method1(self)
% method specific to dataclass1
end
end % end public methods
end % end data_class1
The idea is to have a project class that helps manage overall project information and data classes. There will be some methods in the project class and the data_class classes that are shared. For example, time domain filtering can be generic, and shared across multiple data_class classes. However, there are some methods that are specific to each data_class. Is it possible to have external methods that multiple classes share? I am familiar with using external files for methods by placing myproject.m in a @myproject directory. However, as expected, if I try to add another class to that folder, there is an error that the directory and class name to not agree. The only way I can currently think to implement someting like this is to have the methods in each data_class and myproject call some external static methods.
I dont know if my data_class can inheret only some of the methods from the myproject class or if I make data_class < myproject, it will inheret all the methods and properties.
I may be in horrible violation of basic OOP principles, so please let me know if this is just a major "no-no". Please let me know if this question isn't clear.
Thanks!

  0 Comments

Sign in to comment.

2 Answers

Answer by Steven Lord
on 16 Aug 2019
 Accepted Answer

There are a number of problems with your data_class1 class.
classdef data_class1 < handle %?? - not a subclass, right?
properties (SetAccess = public, GetAccess = public)
data
samplingrate
end
Your data_class1 is a subclass of handle but is not a subclass of your myproject class. It is independent of myproject.
methods (Access = public) % constructor
function self = myproject
The comment is incorrect. The constructor for a class must have the same name as the class itself. Because this method is not called data_class1 it is a method of data_class1 named myproject (though it has a few issues that I'll describe below.)
Class constructors don't need to accept an instance of the class as input. Static methods of a class don't need to accept an instance of the class as input. [In both cases they can but they are not required to.]
Non-static methods must accept an instance of the class as at least one of their inputs. That's how MATLAB knows which implementation of that class method to call. [Suppose there was another class, data_class2, that defined a myproject method. Would myproject() call data_class1's myproject() or data_class2's myproject()? Actually given all your code, it would try to create a myproject() object, ignoring both the data classes.]
Your myproject method is not the constructor for this class, it's not Static, and it accepts no inputs therefore it cannot be called.
self.data = load("data_class1_data.mat") % some file
Rather than loading this (potentially very large) data file once every time this method gets called (assuming you either make it the constructor of the data_class1 class or allow it to accept an input that isa data_class1) I'd store it as a Constant property that gets populated when the class first gets instantiated.
self.samplingrate = 1e500000; %Hz - absurd sampling rate
That sampling rate is not only absurd, it's infinite. It is much, much larger than realmax and so overflows to Inf.
end
end % end constructor
On to your main question:
I dont know if my data_class can inheret only some of the methods from the myproject class or if I make data_class < myproject, it will inheret all the methods and properties.
You can make methods and properties have private Access to restrict their use only to methods of the class that define them, or you can restrict via method and property attributes what classes are allowed to use methods and properties of a class, but for what you're describing I would use inheritance.
classdef book
properties
theText
end
methods
function read(thebook)
% Pass thebook.theText into speech reading software, perhaps?
end
end
end
All book objects can be read and have theText.
classdef murderMystery < book
properties
theVictim
theDetective
end
properties(Hidden, Access=protected)
theMurderer
end
methods
function solve(thebook)
% Have thebook.theDetective reveal the identity of theMurderer!
end
end
end
All murderMystery objects are books, and they have a theVictim, theDetective, and a theMurderer (who the audience doesn't know about, their identity is Hidden!) You can read() a murderMystery (since it isa book) or access its theText, but you can also solve() a murderMystery. Note that you can't solve() an arbitrary book, you can only solve() it if it isa murderMystery.
Make a base class (for this example, book is the base class) and store the properties and methods that all of your data objects must share in it. [Consider making it an Abstract class if it's not meant to be used on its own.] Each subclass can inherit from one or more base classes, getting those properties and methods the base class provides (read(), theText) as well as whatever properties and/or methods (if any) it provides itself (theVictim, theDetective, theMurderer, solve().)

  1 Comment

Thank you for the detailed answer. I saw a few errors in my original post that were definitely wrong (e.g., where I put the constructor comment). I know aspects of my question were completely contrived (e.g. sampling rate), but I was trying to keep it general for my question. At first, inheretiance was not what I wanted because I did not want my data classes to inheret the properties of my project class. What I think will work for me is to create a project class and a general data class. I can create unique instances of my data class for each of my data types. They can inheret all of the methods and properties of my general data class, but can have methods specific to each type.

Sign in to comment.


Answer by Matt J
on 16 Aug 2019
Edited by Matt J
on 16 Aug 2019

Is it possible to have external methods that multiple classes share?
It sounds like the "external methods" you are trying to implement needn't be class methods at all. Why not just make them ordinary Matlab functions with global scope?
Another option would be to put all project and data classdefs in some common parent folder and all functions you wish them to share in a private/ subfolder. Any functions in a private/ subfolder will be seen only by code in the parent folder.

  1 Comment

Thanks, I think you are right. After thinking about it, I don't need external methods. Thanks for the info on private functions. That was new to me.

Sign in to comment.