MATLAB Answers

Jim Hokanson

Why is calling constructor from class above package allowed?

Asked by Jim Hokanson
on 11 Jan 2013

I just noticed that I can call a constructor from an instance of a class that is situated just above the package that the class is in. I haven't done too much testing to fully flush this out but here is a rough example setup which I believes mimics my case.

For example, in my limited testing the following works, assuming the given folder structure:

  • /root/@class1
  • /root/+class1/@class2
x = class1;
x.class2 %calls the constructor of class2.

Is this a bug or a feature? I would expect that this would be a call to a method or property of the instantiated object class1, which if not present, would throw an error.

Thanks, Jim



No products are associated with this question.

1 Answer

Answer by Jennifer Black on 16 Jan 2013
 Accepted answer

Hi Jim,

In your example code you have both a class and a package with the same name ("class1") in the same directory. Given an instance of class1, and a method name that isn’t defined for that class, MATLAB is currently finding the function scoped to the package of the same name as the class of the instance. We consider this behavior a bug that will be removed in a future release. Please don’t rely on this behavior.




Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!