user3245507
user3245507

Reputation: 21

Matlab unable to call superclass method

I'm new to Matlab and am facing some problems with calling superclass methods.

I've got this code:

Superclass test1:

classdef test1 < handle
    methods
        function obj = test1()
        end
        function test2(obj)
            disp(1);
        end
    end

end

Subclass test:

classdef test < test1 & handle
    properties
        foo = 1;
    end
    methods
        function obj = test()
            obj = obj@test1();
        end
        function a = bar(obj)
            superclasses(obj)
            test2@test1(obj)
        end
    end
end

The inheritance works correctly; the superclasses function shows test1 as a superclass of test. However, when I call test2@test1(obj), it returns an error:

"@" Within a method, a superclass method of the same name is called by saying method@superclass. The left operand of "@" must be the method name.

The test2 method obviously exists within the superclass test1, so I'm not sure what exactly is going wrong.

Upvotes: 2

Views: 1856

Answers (1)

Navan
Navan

Reputation: 4477

You can use the @ syntax only if the method names in your superclass and child class are same and the call is within the child class method with the same name. Otherwise you can just call the method directly since there is no confusion. So instead of test2@test1(obj) just use test2(obj).

You also do not need to specify handle as a super class again in your child class.

Upvotes: 1

Related Questions