Abstract Classes and Class Members - MATLAB & Simulink (original) (raw)

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

abstract class — A class that cannot be instantiated, but that defines class components used by subclasses.

abstract members — Properties or methods declared in an abstract class, but implemented in subclasses.

concrete class — A class that can be instantiated. Concrete classes contain no abstract members.

concrete members — Properties or methods that are fully implemented by a class.

interface — An abstract class describing functionality that is common to a group of classes, but that requires unique implementations within each class. The abstract class defines the interface of each subclass without specifying the actual implementation.

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.

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.

MATLAB® does not force subclasses to implement concrete methods with the same signature or attributes.

Abstract classes:

Declare Classes as Abstract

A class is abstract when it declares:

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

For classes that declare the Abstract class attribute:

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) abstMethod(obj) end

For methods that declare the Abstract method attribute:

Abstract Properties

Define an abstract property:

properties (Abstract) AbsProp end

For properties that declare the Abstract property attribute:

For more information on access methods, see Property Get and Set Methods.

Determine If a Class Is Abstract

Determine if a class is abstract by querying the Abstract property of its matlab.metadata.Class object. For example, theAbsClass defines two abstract methods:

classdef AbsClass methods(Abstract) result = absMethodOne(obj) output = absMethodTwo(obj) end end

Use the logical value of the matlab.metadata.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 matlab.metadata.abstractDetails function to display the names of abstract properties or methods and the names of the defining classes:

matlab.metadata.abstractDetails('AbsClass');

Abstract methods for class AbsClass: absMethodTwo % defined in AbsClass absMethodOne % defined in AbsClass

Find Inherited Abstract Properties and Methods

The matlab.metadata.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 methods function out = absMethodTwo(obj) ... end end end

Determine if you implemented all inherited class members usingmatlab.metadata.abstractDetails:

matlab.metadata.abstractDetails(?SubAbsClass)

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; msub.Abstract

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