Why does inheritance in MATLAB behave differently from other OOP languages?

2 views (last 30 days)
The standard implementation of inheritance in most programming languages (like C++, Java) has protected members accessible to:
1) members and friends of that class;
2) members of any derived class of that class. Every derived class object has a base class object associated with it. The derived class object can only access protected members of that base class object. This accessibility extends to all derived classes of that derived class.
MATLAB appears to use a different implementation where a protected member of a class is only accessible to:
1) members and friends of that class;
2) members of any derived class of that class.
Below is sample code that highlights this difference:
classdef Base
properties (Access = protected)
mProtected = 1;
end
end
classdef Derived < Base
end
classdef Derived2 < Base
methods
function obj = Derived2
obj = obj@Base;
obj.mProtected = 0; % this works, as expected
newBase = Base;
newDerived = Derived;
obj.mProtected = newBase.mProtected; % MATLAB allows this, but shouldn't
obj.mProtected = newDerived.mProtected; % MATLAB allows this, but shouldn't
end
end
end
Is my understanding correct?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Jul 2023
When MATLAB evaluates whether a given property can be accessed, it looks at two things:
1. Does the property exist for the given object?
2. Does the context (the block within which a variable is defined) have access to the property?
In the above code, the constructor of the ‘Derived2’ class creates an instance of the ‘Derived’ class, and then uses that new instance to access the ‘mProtected’ property. Here, the 'mProtected' properties of the 'Base' and 'Derived' classes exists for the 'Derived2' object and are also accessible in the context of the 'Dervied2' class constructor. 
First, because the ‘mProtected’ property is 'protected' (as opposed to 'private'), both ‘Derived’ and ‘Derived2’ classes inherit the ‘mProtected’ property from their parent and it is legal to reference the property as long as the context where the reference occurs has access. Second, because the property is 'protected', it is valid to reference it only within a method of the defining class or a method of a subclass. Here, the property is being referenced in the constructor of the ‘Derived2’ class. As such, the references using instances of ‘Derived’ and ‘Base’ classes are both valid.
Unlike languages such as C++, MATLAB does not have a notion of a ‘this’ instance. There is no special preference afforded to the first input to a method or the object being constructed. Whether a property or method is accessible to a given context is based on the class of a value and how the member being accessed is defined. One can pass multiple instances of the same class to a method and all will have access to the same methods and properties. 

More Answers (0)

Categories

Find more on Methods in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!