Main Content

Modify Inherited Properties

Superclass Property Modification

There are two separate conditions under which you can redefine superclass properties:

  • The value of the superclass property Abstract attribute is true

  • The values of the superclass property SetAccess and GetAccess attributes are private

If a superclass defines a property as abstract, the subclass must implement a concrete version of this property or the subclass is also abstract. Superclasses define abstract properties to create a consistent interface among subclasses.

If a superclass defines a property with private access, then only the superclass can access this property. The subclass can implement a different property with the same name.

Private Local Property Takes Precedence in Method

When superclass and subclass define a property with the same name, methods that refer to this property access the property of the class defining the method.

For example, if a subclass property has the same name as a superclass private property, and a method of the superclass references the property name, MATLAB® accesses the property defined by the superclass.

Consider the following classes, Super and Sub:

classdef Super
   properties (Access = private)
      Prop = 2
   end
   methods
      function p = superMethod(obj)
         p = obj.Prop;
      end
   end
end
classdef Sub < Super
   properties
      Prop = 1
   end
end

If you create an instance of the Sub class and use it to call the superclass method, MATLAB accesses the private property of the superclass:

subObj = Sub
subObj = 

  Sub with properties:

    Prop: 1
subObj.superMethod
ans =

     2

Related Topics