Set properties from Superclass in subclass

Hello, I have defined a superclass 'food.m' that has certain properties:
classdef food
properties
vitamins
protein
carbs
end
end
Now I want to define a subclass that inherits from the above, and also sets the vitamins property (in other words, I will want all instances of this subclass to have the property 'Vitamins' set to "C")
classdef fruit < food
properties (Access = private)
vitamins = "C"
end
end
The following error is raised
%Cannot define property 'vitamins' in class 'fruit' because the property has already been defined in the
%superclass 'food'.
How do I fix this?
Thank you in advance for your help!

 Respuesta aceptada

Turlough Hughes
Turlough Hughes el 20 de Ag. de 2021
Editada: Turlough Hughes el 20 de Ag. de 2021
Set the Access attribute in the superclass:
classdef food
properties
protein
carbs
end
properties (Access = private)
vitamins
end
end
otherwise, the default is that vitamins is defined as a public property in the superclass.
You can get a list of default property attributes here.

8 comentarios

elevonm
elevonm el 20 de Ag. de 2021
Wow that was easy, thank you very much!!
However now a second problem arises. I actually want all my properties in my superclass to be private (for convenience in other subclasses).
classdef food
properties (Access = private)
vitamins
protein
carbs
end
end
Now, if I only define the vitamins in 'fruit' as such:
classdef fruit < food
properties
vitamins = "C"
end
end
and define an instance of the class
apple = fruit
I only obtain the vitamins property. I want all other properties (protein, carbs) to also appear, automatically set to 0. How do I achieve that?
Thank you
Turlough Hughes
Turlough Hughes el 20 de Ag. de 2021
Editada: Turlough Hughes el 20 de Ag. de 2021
Ah, I think what you're looking for is to have the SetAccess=private in the superclass. This means you restrict options to set the properties, but you can still "Get" them and hence see them when the object is instantiated, (i.e, the GetAcess attribute stays public, which is the default).
classdef food
properties (SetAccess = private)
vitamins = "C"
protein = 0
carbs = 0
end
end
Turlough Hughes
Turlough Hughes el 20 de Ag. de 2021
and in this way you don't need to define the properties in the subclass.
elevonm
elevonm el 20 de Ag. de 2021
ok, but what if I have another subclass 'bread' in which I want 'vitamins' to be 0, but carbs and protein to be defined seperately in the subclass 'bread'? Without interfering with my 'fruit' class...
Turlough Hughes
Turlough Hughes el 20 de Ag. de 2021
Editada: Turlough Hughes el 20 de Ag. de 2021
In that case the SetAccess is protected. This means the subclass has access to change the property values while default values get defined in the superclass. In the following example, the user cannot change the properties (they can only view them), and a subclass can redine property values as required (typically in the subclass's constructor). See the following:
food (superclass):
classdef food
properties (SetAccess = protected)
vitamins = "C"
protein = 0
carbs = 0
end
end
bread (subclass):
classdef bread < food
methods
function obj = bread
obj.carbs = "lots";
obj.vitamins = 0;
end
end
end
elevonm
elevonm el 20 de Ag. de 2021
Thank you very much for your help!
Turlough Hughes
Turlough Hughes el 20 de Ag. de 2021
Happy to help :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2021a

Preguntada:

el 20 de Ag. de 2021

Comentada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by