Why use classes?
If you’ve followed this Enhanced Matlab Plotting Series, it should be apparent that the mPlot class we have created is the definition for a custom object. That is, we have created an object (strictly speaking its the definition for an object) that has user-defined properties (like fields within a struct) and user-defined methods (functions) attached to the object. If you started this series with no knowledge of classes or object-oriented programming, then some portions of this series may have been a bit fuzzy…..and I’m glad you’ve stayed tuned for this long as this part of the series we are going to point out some of the obvious and not so obvious advantages of using object-oriented programming (OOP). I should mention, that the syntax for OOP used in these examples was released in Matlab 2008a. So this functionality is not available in previous versions of Matlab. FYI, you may see individuals commenting on classes in Matlab before 2008a. The syntax and methodology for using those classes is still supported in later versions of Matlab, but will not be used in these tutorials.
Make the problem example driven
This part of the series we are going to focus on inheritance. That is, we are going derive another plotting class from our mPlot class. Instead of going through all of the generalized talk, lets use a practical programming problem to understand inheritance. If you’re familiar with the mPlot class, you know that back on day one we pre-defined a number of marker and line colors. Check out the method buildProperties() if you’re not following – https://matlabmafia.wordpress.com/2010/11/27/enhanced-matlab-plotting-series-part-i/. Lets suppose that others may not share my taste in color selection. So you have the mPlot class, but can’t live with the pre-defined colors. Lets also say that you want to automatically enable auto-incrementing via the isAutoIncrementing property (Note that this is currently disable in the mPlot class definition we have been using). Therefore our goal is to 1) redefine all of the colors inside the mPlot class and 2) enable auto-incrementing upon instantiation, but retain all of the other class functionality.
Why use inheritance??
Before we actually dive into code and attain our goal, lets answer the “why use inheritance” question; because I know someone out there is saying “its simple, just delete all of the colors inside of the buildProperties() method and type in new colors.” While it is true that this would work, inheritance provides us with a much more dynamic and robust solution.
Lets say for example you choose to alter the color scheme and change the class name from mPlot to newColorPlot; by alter, I mean physically delete all of the color definitions and replace them with new definitions. So now we have a class named newColorPlot with the desired colors. But what happens when the creator of mPlot releases a new version with extra functionality and bug fixes? Well, one might consider tediously merging the two files into a composite file that functions with the new mPlot functionality, but has the newColorPlot colors. While this would work, it would be time consuming and could possibly lead to errors. The other option is to completely ignore the update to mPlot. But in this case we risk leaving bugs throughout our newColorPlot class. Inheritance gives us the best of both worlds. It means we can implicitly accept all of the new functionality and bug fixes from mPlot without touching any code. All that needs to be done is replace the old mPlot class definition file with a new one.
If you want more detail, check out Matlab’s website regarding inheritance and building on other classes at http://www.mathworks.com/help/techdoc/matlab_oop/brdf7kt.html.
The Code – Inheriting
Lets start off by simply creating a new class that inherits from the mPlot class. This is show below. Notice that in the first line we have used a logical “and” (aka “&”) to inherit both the handle class (don’t worry about that for now) and the mPlot class. So there you go, we’ve inherited from a class.
Next, lets compare an instance of mPlot with an instance of our new class that inherits from mPlot. They better be the same….that is, the must have the same properties and methods. This is shown below.
The Code – Adding Functionality
Now that we have the new class, lets redefine the colors. For simplicity, the jet function is used to automatically generate a set of colors. We’ve also enabled auto-incrementing by default. As a side note, what is actually happening is the mPlot constructor is being executed once the myInheritedmPlot object is instantiated. Next, the myInheritedmPlot constructor is being executed. This overwrites all of the marker edge colors and marker face colors. It also changes the value of the isAutoIncrementing property.
Simple as that….we are finished.
Example – Our new class in action
Here we are simply going to plot with an mPlot object and a myInheritedmPlot object to show that they have different colors and that the auto-incrementing state is enabled by default. The code below
produces the plot.
Its evident that both of the objects have behaved in a similar fashion, but the color scheme has now been changed.
In this simple example, we derived myInheritedmPlot class from the mPlot class definition. The color scheme of the new class was altered. Substantial more functionality could have been explored, but this simple example gets one on their way to creating derived classes. Stay tuned!