MATLAB Object-Oriented Programming

(Joyce) #1

Define an Interface Superclass


In this section...
“Interfaces” on page 12-96
“Interface Class Implementing Graphs” on page 12-96

Interfaces


The properties and methods defined by a class form the interface that determines how
class users interact with objects of the class. When creating a group of related classes,
interfaces define a common interface to all these classes. The actual implementations of
the interface can differ from one class to another.

Consider a set of classes designed to represent various types of graphs. All classes must
implement a Data property to contain the data used to generate the graph. However, the
form of the data can differ considerably from one type of graph to another. Each class can
implement the Data property differently.

The same differences apply to methods. All classes can have a draw method that creates
the graph, but the implementation of this method changes with the type of graph.

The basic idea of an interface class is to specify the properties and methods that each
subclass must implement without defining the actual implementation. This approach
enables you to enforce a consistent interface to a group of related objects. As you add
more classes in the future, the interface remains the same.

Interface Class Implementing Graphs


This example creates an interface for classes used to represent specialized graphs. The
interface is an abstract class that defines properties and methods that the subclasses
must implement, but does not specify how to implement these components.

This approach enforces the use of a consistent interface while providing the necessary
flexibility to implement the internal workings of each specialized subclass differently.

In this example, a package folder contains the interface, derived subclasses, and a utility
function:

+graphics/GraphInterface.m % abstract interface class
+graphics/LineGraph.m % concrete subclass

12 How to Build on Other Classes

Free download pdf