Abstract classes are useful for describing functionality that is common to a group of classes, but requires unique implementations within each class.
An abstract class serves as a basis (that is, a superclass) for a group of related subclasses. An abstract class can define abstract properties and methods that subclasses implement. Each subclass can implement the concrete properties and methods in a way that supports their specific requirements.
A subclass must implement all inherited abstract properties and methods to become a concrete class. Otherwise, the subclass is itself an abstract class.
MATLAB® does not force subclasses to implement concrete methods with the same signature or attributes.
Can define properties and methods that are not abstract
Pass on their concrete members through inheritance
Do not need to define any abstract members
A class is abstract when it declares:
Abstract class attribute
An abstract method
An abstract property
If a subclass of an abstract class does not define concrete implementations for all inherited abstract methods or properties, it is also abstract.
Declare a class as abstract in the
classdef (Abstract) AbsClass ... end
For classes that declare the
Abstract class attribute:
Concrete subclasses must redefine any properties or methods that are declared as abstract.
The abstract class does not need to define any abstract methods or properties.
When you define any abstract methods or properties, MATLAB automatically sets the class
Abstract attribute to
Define an abstract method:
methods (Abstract) abstMethod(obj) end
For methods that declare the
Abstract method attribute:
Do not use a
function...end block to define an abstract method, use only the method signature.
Abstract methods have no implementation in the abstract class.
Concrete subclasses are not required to support the same number of input and output arguments and do not need to use the same argument names. However, subclasses generally use the same signature when implementing their version of the method.
Abstract methods cannot define
Define an abstract property:
properties (Abstract) AbsProp end
For properties that declare the
Abstract property attribute:
Concrete subclasses must redefine abstract properties without the
Concrete subclasses must use the same values for the
GetAccess attributes as those attributes used in the abstract superclass.
Abstract properties cannot define access methods and cannot specify initial values. The subclass that defines the concrete property can create access methods and specify initial values.
For more information on access methods, see Property Access Methods.
Determine if a class is abstract by querying the
Abstract property of its
meta.class object. For example, the
AbsClass defines two abstract methods:
classdef AbsClass methods(Abstract) result = absMethodOne(obj) output = absMethodTwo(obj) end end
Use the logical value of the
Abstract property to determine if the class is abstract:
mc = ?AbsClass; if ~mc.Abstract % not an abstract class end
meta.abstractDetails function to display the names of abstract properties or methods and the names of the defining classes:
Abstract methods for class AbsClass: absMethodTwo % defined in AbsClass absMethodOne % defined in AbsClass
meta.abstractDetails function returns the names and defining class of any inherited abstract properties or methods that you have not implemented in your subclass. Use this function if you want the subclass to be concrete and must determine what abstract members the subclass inherits.
For example, suppose that you create a subclass of the
AbsClass class that is defined in the previous section. In this case, the subclass implements only one of the abstract methods defined by
classdef SubAbsClass < AbsClass % Does not implement absMethodOne % defined as abstract in AbsClass methods function out = absMethodTwo(obj) ... end end end
Determine if you implemented all inherited class members using
Abstract methods for class SubAbsClass: absMethodOne % defined in AbsClass
SubAbsClass class is abstract because it has not implemented the
absMethodOne method defined in
msub = ?SubAbsClass; msub.Abstract
ans = 1
If you implement both methods defined in
AbsClass, the subclass becomes concrete.