Defining Abstract Classes

Abstract Classes

Abstract classes are useful for describing functionality that is common to a group of classes, but requires unique implementations within each class.

 Abstract Class Terminology

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 must implement. Each subclass can implement the concrete properties and methods in a way that supports their specific requirements.

Abstract classes can define properties and methods that are not abstract, and do not need to define any abstract members. Abstract classes pass on their concrete members through inheritance.

Implementing a Concrete Subclass

A subclass must implement all inherited abstract properties and methods to become a concrete class. Otherwise, the subclass is itself an abstract class.

Declaring Classes as Abstract

A class is abstract when it declares:

  • An abstract method

  • An abstract property

  • The Abstract class attribute

A subclass of an abstract class is itself abstract if it does not define concrete implementations for all inherited abstract methods or properties.

Abstract Methods

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 Properties

Define an abstract property:

properties (Abstract)
   AbsProp
end

For properties that declare the Abstract property attribute:

  • Concrete subclasses must redefine abstract properties without the Abstract attribute, and must use the same values for the SetAccess and GetAccess attributes as those used in the abstract superclass.

  • Abstract properties cannot define set or get access methods (see Property Access Methods) and cannot specify initial values. The subclass that defines the concrete property can create set or get access methods and specify initial values.

Abstract Class

Declare a class as abstract in the classdef statement:

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 true.

Determine If a Class Is Abstract

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, Static)
      result = absMethodOne
      output = absMethodTwo
   end
end

Use the logical value of the meta.class Abstract property to determine if the class is abstract:

mc = ?AbsClass;
if ~mc.Abstract
   % not an abstract class
end

Display Abstract Member Names

Use the meta.abstractDetails function to display the names of abstract properties or methods and the names of the defining classes:

meta.abstractDetails('AbsClass');
Abstract methods for class AbsClass:
   absMethodTwo   % defined in AbsClass
   absMethodOne   % defined in AbsClass

Find Inherited Abstract Properties and Methods

The meta.abstractDetails function returns the names and defining class of any inherited abstract properties or methods that you have not implemented in your subclass. This can be useful if you want the subclass to be concrete and need to determine what abstract members the subclass inherits.

For example, suppose you subclass AbsClass, which is described in the Determine If a Class Is Abstract section:

classdef SubAbsClass < AbsClass
% Failed to implement absMethodOne
% defined as abstract in AbsClass
   methods (Static)
      function out = absMethodTwo(a,b)
         out = a + b;
      end
   end
end

Determine if you implemented all inherited class members using meta.abstractDetails:

meta.abstractDetails(?SubAbsClass)

Abstract methods for class SubAbsClass:
   absMethodOne   % defined in AbsClass

The SubAbsClass class itself is abstract because it has not implemented the absMethodOne defined in AbsClass.

Was this topic helpful?