Abstract Classes and Class Members

Abstract Classes

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:

Can define properties and methods that are not abstract

Pass on their concrete members through inheritance

Do not need to define any abstract members

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

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.

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.

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic. By continuing to use this website, you consent to our use of cookies. Please see our Privacy Policy to learn more about cookies and how to change your settings.