Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Inheritance from classes withina package
Replies: 7   Last Post: May 24, 2013 3:57 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: Inheritance from classes withina package
Posted: May 23, 2013 11:13 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"Ron Lu" <dlu@masimo.com> wrote in message
news:knjldr$1rs$1@newscl01ah.mathworks.com...
> "Nick Clark" wrote in message <h9dqf5$e15$1@fred.mathworks.com>...
>> Hi all,
>> I have a query regarding inheritance of one class by another within a
>> package. For example, classB < classA within package mypack. From outside
>> the package directory I type:
>>
>> import mypack.*
>> x = classB
>>
>> . . then I get an error saying that classA cannot be accessed. I can
>> resolve this by making classB < myclass.classA at the top of the classdef
>> of classB. However, this seems like a bit of a clunky 'duct tape' kind of
>> work around. It also causes problems when trying to display the
>> documentation about the classes which are linked in blue from the disp
>> command.
>>
>> What is the recommended elegant method for inheritance within a package?

>
> Anyone can help?
>
> I ran into the exact same problem.
> I already finished all my classes and now want to put them into different
> packages. And I HAVE TO add those import statements everywhere, or add
> packageName.Class* to let the class know where to find dependencies in the
> same package.


That is correct.

> Is there a way to work around?

No. Packages define a namespace, and the package name is sort of part of the
name of the class.

As a concrete example, if you had this directory structure (with mydirectory
on the path):

mydirectory\classB.m
mydirectory\+mypack\classA.m
mydirectory\+mypack\classB.m

and you wanted to create an instance of the class defined in the classB.m
file inside +myPack from within the classA.m file, you would need to use
mypack.classB. If instead you wanted to instantiate an instance of the
mydirectory\classB.m class, you would simply use classB. [If you imported
mypack.classB inside classA.m, I don't believe you'd be able to access the
classB class defined outside the package in the function in which you
imported.]

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.