Most programming languages are either procedural or object-oriented. Procedural programs are comprised of functions, each of which performs a task. Object-oriented programs use classes, which contain both data and functions to manipulate the data. Hybrid languages can utilize both of these programming paradigms. All of our programs so far have been procedural, but the MATLAB® software uses objects in its graphics, and thus has object-oriented programming ( OOP) capabilities.
In this chapter, we will first introduce some of the concepts and terminologies of OOP using graphics objects and will show how to manipulate plot properties using this system. Later we will show how user-defined classes can be created.
Object-Oriented Programming
This short section is intended to introduce the very basic ideas behind OOP as well as some of the terminology that is used. This section is very dense in terms of the terminology. It is hoped that by introducing the terms here, and then giving examples in the next two sections, the terms will be easier to understand.
Built-in data types have certain capabilities associated with them. For example, we can perform mathematical operations such as adding and dividing with number types such as double. When a variable is created that stores a value of a particular type, operations can then be performed that are suitable for that type.
Similarly, abstract data types are data types that are defined by both data and operational capabilities. In MATLAB, these are called classes. Classes define both the data and the functions that manipulate the data. Once a class has been defined, objects can be created of the class.
To define a class, both the data and the functions that manipulate the data must be defined. The data are called properties, and are similar to variables in that they store the values. The functions are called methods. A class definition consists of the definition of the properties and the definition of the methods.
Once a class has been defined, objects can be created from the class. The objects are called instances of the class and an object that is created is an instantiation of the class. The properties and methods of the object can be referenced using the object name and the dot operator.
Inheritance is when one class is derived from another. The initial class is called the base, parent, or superclass, and the derived class is called the derived, child, or subclass. A subclass is a new class that has the properties and methods of the superclass (this is what is called inheritance), plus it can have its own properties and methods. The methods in a subclass can override methods from the superclass if they have the same name.
MATLAB has built-in classes and also allows for user-defined classes. There are two types of classes in MATLAB: value classes and handle classes. The differences will be explained in Section 3. MATLAB uses handle classes to implement graphical objects used in plots.