This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.

Declare Classes as Abstract

A class is abstract when it declares:

  • The 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.

Abstract Class

Declare a class as abstract in the classdef statement:

classdef (Abstract) AbsClass 

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.

Abstract Methods

Define an abstract method:

methods (Abstract)

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)

For properties that declare the Abstract property attribute:

  • Concrete subclasses must redefine abstract properties without the Abstract attribute.

  • Concrete subclasses must use the same values for the SetAccess and 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

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
      result = absMethodOne(obj)
      output = absMethodTwo(obj)

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

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:

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

classdef SubAbsClass < AbsClass
% Does not implement absMethodOne
% defined as abstract in AbsClass
      function out = absMethodTwo(obj)

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

Abstract methods for class SubAbsClass:
   absMethodOne   % defined in AbsClass

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

msub = ?SubAbsClass;
ans =


If you implement both methods defined in AbsClass, the subclass becomes concrete.

Related Examples

Was this topic helpful?