How to change the default Value of superclass property in subclass

Is there any way to change the default value of a super class property in the property block of a subclass? I know I can change it in the constructor method, but the semantics of that doesn't seem right. Also making the property abstract in the super class is inconvenient because then you can't really directly instantiate the superclass. is there any solution to this?

5 Comments

Elaborate on the circumstances in which this change would be made. Otherwise, I could answer "just edit the classdef file".
That is what I mean in the classdef file. To elaborate for example in the superclass I have a property CalculationMode=1; which is effectively and option used by a method of the superclass. Now in the sub class I want the value of CalculationMode=2; so that the superclass method that does the calculating uses its no default Calculation mode. Now I know I can change that volume in the sub class constructor, but there are many properties that have the same situation and changing them in the sub class constructor seems like less readable/obvious code. Does that make sense?
No, it doesn't make sense. Please provide the code for a simple superclass and subclass and explain what you want to do with that code. Please edit the original question to include the code and don't post it as a comment.
Also explain what you mean by "I know I can change it in the constructor method, but the semantics of that doesn't seem right." I don't see how there could be anything semantically wrong with setting the value in the constructor. It seems like the most natural thing to do.
This is what I would like to do
classdef superclass < handle
properties
CalculateMode=1; %1=option1 2=option2
end
end
classdef subclass < superclass
properties
CalculateMode=2;
end
end

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 3 Oct 2012
Edited: Matt J on 3 Oct 2012
Abandon this and set the value of CalculateMode in the constructor. It's the appropriate thing to do.

More Answers (2)

Overriding a superclass property with a subclass property involes a few hurdles, but overwriting a superclass method with a subclass property is simple. This allows the superclass to define a set of defaults, and allows the subclasses to modify those defaults without requiring the subclasses to repeat the same lines of code in each constructor.
The concept in its absolute simplest form:
classdef Super
properties
CalculateMode
end
methods
function obj=Super
obj.CalculateMode=obj.CalculateModeSetting;
end
function out=CalculateModeSetting(~)
out=1;
end
end
end
classdef Sub < Super
properties
CalculateModeSetting=2
end
end
Which gives:
>> Super
ans =
Super with properties:
CalculateMode: 1
>> Sub
ans =
Sub with properties:
CalculateModeSetting: 2
CalculateMode: 2
The only other way I can think to do this is to have the superclass constructor take an CalculateMode as an optional argument. Then the subclass constructor would pass the mode to the superclass.

Products

Community Treasure Hunt

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

Start Hunting!