When inheriting from multiple classes, use the &
character to indicate the combination of the superclasses:
classdef ClassName < SuperClass1 & SuperClass2
For more information on class syntax, see Subclass Syntax.
When you create a subclass derived from multiple superclasses, the subclass inherits the properties, methods, and events defined by all specified superclasses. If more than one superclass defines a property, method, or event having the same name, there must be an unambiguous resolution to the multiple definitions. You cannot derive a subclass from any two or more classes that define incompatible class members.
Here are various situations where you can resolve name and definition conflicts.
If two or more superclasses define a property with the same name, then at least one of the following must be true:
All, or all but one of the properties must have their SetAccess
and GetAccess
attributes set to private
The properties have the same definition in all superclasses (for example, when all superclasses inherited the property from a common base class)
If two or more superclasses define methods with the same name, then at least one of the following must be true:
The method Access
attribute is private
so only the defining superclass can access the method.
The method has the same definition in all subclasses. This situation can occur when all superclasses inherit the method from a common base class and none of the superclasses override the inherited definition.
The subclass redefines the method to disambiguate the multiple definitions across all superclasses. Therefore, the superclass methods must not have their Sealed
attribute set to true
.
Only one superclass defines the method as Sealed
, in which case, the subclass adopts the sealed method definition.
The superclasses define the methods as Abstract
and rely on the subclass to define the method.
If two or more superclasses define events with the same name, then at least one of the following must be true:
The event ListenAccess
and NotifyAccess
attributes must be private
.
The event has the same definition in all superclasses (for example, when all superclasses inherited the event from a common base class)
Resolving the potential conflicts involved when defining a subclass from multiple classes often reduces the value of this approach. For example, problems can arise when you enhance superclasses in future versions and introduce new conflicts.
Reduce potential problems by implementing only one unrestricted superclass. In all other superclasses, all methods are
Abstract
Defined by a subclass
Inherited from the unrestricted superclass
When using multiple inheritance, ensure that all superclasses remain free of conflicts in definition.