Can I import classes in an interface class in matlab?
Show older comments
Hello everyone! Can I import other classes like BaseStation,Technology and Area in the interface class BsGridFactory which are in the datatypes package and the interface class is in another package? or I should import this classes in the implementation functions of subclasses of BsGridFactory?
createBaseStationGrid takes a users parameter which is an array of User objects, an area which is an Area object and a technology which is Technology object. Is the interface class written ok or should I remove the import statements? Thanks in advance!

Accepted Answer
More Answers (1)
Sean de Wolski
on 11 Jan 2018
You can import inside of the individual methods of a class but not globally for the whole class.
classdef x
methods
function foo(obj)
import whatever.myclass
myclass(3)
end
end
end
5 Comments
ana take
on 11 Jan 2018
Guillaume
on 11 Jan 2018
"You can import inside of the individual methods of a class but not globally for the whole class".
Oh yes, I'd forgotten about that. Major pain, that is!
My point still stand, within the context of an interface, where everything is abstract, there is no point in importing anything.
Sean de Wolski
on 12 Jan 2018
Edited: Sean de Wolski
on 12 Jan 2018
One reason is to use short names. I do this quite frequently, especially for nested packages. Import a whole package:
import pack.subpack.*
p = subpackitem1;
p2 = subpackitem2;
I added my vote for the import long_name as short_name interface as well.
Ashley Trowell
on 16 Jul 2021
Unfortunately, the import statement essentially obfuscates code, since it breaks the Ctrl + D functionality. The IDE doesn't seem to know where the code is defined. If you Ctrl + D on a imported function, you are told that it hasn't been defined, or are directed to a non package version, a version that won't actually be executed at runtime.
An import statement is critical for efficient development, but is a seriously broken feature in Matlab.
Nikolaus Koopmann
on 2 Aug 2023
Edited: Nikolaus Koopmann
on 2 Aug 2023
This! One of the most annoying aspects of writing OOP code in Matlab..
Categories
Find more on Software Development in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!