Where does the `empty` method come from?

3 views (last 30 days)
The code come from this book 4.1.3
Then input mc=?SubClass in command line and open mc-->MethodList in workspace,you will find two method, one is 'SubClass' ,another is 'empty'.Where does the `empty` method come from? I don't built a `empty` methoed? Is it build-in method? I have doubts about this viewpoint.
Because you can input this in command line:
A=SubClass(1);
B=methods(A)
you will find there is just one method in object A.
Questions:
1 So Where does the `empty` method come from? And Why can't I find `empty` method using the `method` command in object A?"
2 The code obj=obj@Base(value); in SubClass.m. What does this code mean, and what is the special function of the '@' symbol?It doesn't appear to be a regular assignment statement.
classdef Base
properties(Access=private)
a;
end
methods
function obj=Base(value)
obj.a=value;
end
end
methods (Access=private)
function Fun(obj)
disp(num2str(obj.a));
end
end
end
classdef SubClass < Base
methods
function obj=SubClass(value)
obj=obj@Base(value);
end
end
end

Accepted Answer

Steven Lord
Steven Lord on 20 Aug 2023
For question 1, as Bruno Luong wrote in this other question you asked,
'In this empty doc page one can read "empty is a hidden, public, static method of all nonabstract MATLAB® classes. You can override the empty method in class definitions."'
If you don't specifically define an empty method for your class, MATLAB will use a "default" Hidden (meaning it doesn't show up in the output of methods), public Access, Static (so you can call it without an instance of the class as input) method.
For question 2, that's how a subclass can call a superclass method (in this case the constructor of the superclass.) See this documentation page for more information about the limitations of that syntax.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!