Why does inheritance in MATLAB behave differently from other OOP languages?
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Deploy to Java Applications Using MWArray Data API finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!