(I'm answering from a MATLAB perspective here because this is what I know best, but Octave has the exact same behavior here, so it applies equally to Octave.)
The problem with @
-style classes is that MATLAB doesn't know their properties until an object of the class is created. Thus, to use an old-style class as a base class, MATLAB would have to construct a base object just to learn what the class looks like, but calling the constructor with wrong inputs could lead to an error message. Or the constructor could do hours worth of work. It is not viable to build an object just to learn what it looks like.
I think this is the core reason for introducing the classdef
-style classes. There are other improvements, but none as important as this one. In @
-style classes inheritance is determined at the time of object creation, and one must manually create objects of the base class first, and merge those into the derived object being created.
Here's an interesting example for an @
-style class behavior that makes it impossible to use it as a base class in a classdef
-style class:
@foo/foo.m:
function obj = foo(x)
if x
obj = class(struct('a','a'),'foo');
else
obj = class(struct('b','b'),'foo');
end
Now in MATLAB we type:
>> a=foo(0)
a =
foo object: 1-by-1
>> b=foo(1)
Error using class
[...]
>> clear classes
>> b=foo(1)
b =
foo object: 1-by-1
>> a=foo(0)
Error using class
[...]
Class foo
changes depending on how the first object of the class is created. Once we've created an object in one way, the other way becomes illegal.
A solution to OP's original problem:
In Octave 4.0 the ss
class is defined in the old style with @ss
directory. I want to derive a class with a customized __freqresp__
method [...].
Instead of deriving a new class with a customized method, think about overriding the existing method. Simply create a directory @ss
, and put a file __freqresp__.m
in it. Make sure that your @ss
directory lives in a directory on the Octave path that comes before the toolbox directory where the original class lives.
I'm assuming that the original __freqresp__.m
is an actual method, and not a function in the class' private
subdirectory. If so, it's not a method and cannot be overridden (see Function Precedence in the Octave manual).
Note that you can override overloaded functions for any type, even built-in types. For example you can create a function @double/length.m
to override the length
function when called with a normal (double) matrix.