1

I read this documentation page about how to invoke superclass constructor from a child class. The syntax they mention is this:

obj = obj@MySuperClass(SuperClassArguments);

I'm wondering what the purpose of @ symbol in the above syntax is. Is the @ symbol just a meaningless place occupier in the syntax or does the @ symbol represent the function handle symbol in MATLAB?

If I use:

obj = MySuperClass(SuperClassArguments); 

instead of

obj = obj@MySuperClass(SuperClassArguments);

it still works fine. So what is the purpose of using @ symbol?

chappjc
  • 30,359
  • 6
  • 75
  • 132
user2166888
  • 579
  • 1
  • 5
  • 13
  • I actually didn't realize you linked the function handle documentation. But the @ character certainly represents the function handle. – voxeloctree Jul 19 '13 at 20:00
  • Yikes. Who makes up these syntaxes? Is there no one at The MathWorks whose job it is to maintain the consistency the language? – horchler Jul 19 '13 at 20:19

1 Answers1

6

1) no this has nothing to do with function handles, this is the syntax used to call the superclass constructor

2) you could try it and see for yourself. Here is an example:

A.m

classdef A < handle
    properties
        a = 1
    end
    methods
        function obj = A()
            disp('A ctor')
        end
    end
end

B.m

classdef B < A
    properties
        b = 2
    end
    methods
        function obj = B()
            obj = obj@A();        %# correct way
            %#obj = A();          %# wrong way
            disp('B ctor')
        end
    end
end

With the correct syntax, we get:

>> b = B()
A ctor
B ctor
b = 
  B with properties:

    b: 2
    a: 1

If you use the commented line instead of the first one, you get the following error:

>> clear classes
>> b = B()
A ctor
A ctor
B ctor
When constructing an instance of class 'B', the constructor must preserve the class of the returned
object.
Error in B (line 8)
            disp('B ctor') 
Amro
  • 123,847
  • 25
  • 243
  • 454