MATLAB method overriding behaviour: choose method from this class and not from the subclass - matlab

Here are two example classes A and B. The class B is a subclass of A and overrides the method "myMethod":
classdef A
methods
function this = A()
this.myMethod();
end
function myMethod(this)
fprintf('A:myMethod\n');
end
end
end
classdef B < A
methods
function this = B()
this#A();
this.myMethod();
end
% Overrides 'myMethod' in A
function myMethod(this)
fprintf('B:myMethod\n');
end
end
end
Now, when I create an object of class B, the output is:
>> B();
B:myMethod
B:myMethod
My question is: How can I modify the constructor of class A such that the method from class A is called instead of the method from the subclass. The output should become:
>> B();
A:myMethod
B:myMethod
I have tried to do
this.myMethod#A();
in the constructor of A but it gives me an error saying that it only works for super class calls.
Thank you for the help,
Adrian

Your syntax is correct but you cannot call superclass methods from the subclass constructor. If you use the same syntax in a different method in B() it will work fine. E.g., if we redefine class B() to look like this:
classdef B < A
methods
function this = B()
this = this#A();
end
% Overrides 'myMethod' in A
function myMethod(this)
fprintf('B:myMethod\n');
this.myMethod#A() ;
end
end
end
we get
>> B()
B:myMethod
A:myMethod
ans =
B with no properties.
The only superclass method you can call from a subclass constructor is the superclass constructor.

Related

Inheritance Polymorphism In Matlab

I was trying to read the matlab docs for a while, and either this doesn't exist or they named it something that did not occur to me.
In mainstream languages such as Java for example, I can implement the Strategy pattern with simple polymorphism like so:
class A{
void foo(){
System.out.println("A");
}
}
class B : A{
void foo(){
System.out.println("B");
}
}
A ab = new B();
ab.foo();//prints B, although static type is A.
the same concept is also available in interpreter languages such as python.
Is there something like this in Matlab (I'm using 2016a).
What is it called? what is the syntax?
classdef A < handle
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
methods
function obj = A ()
end
function printSomething (obj)
disp ('Object of class A') ;
end
end
end
classdef B < A
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
methods
function obj = B ()
end
function printSomething (obj)
disp ('Object of class B');
end
end
end
creation of instance of class A:
a = A () ;
a.printSomething ();
By executing above line of code, you will see:
Object of class A
creation of instance of class B:
b = B () ;
b.printSomething()
By executing above line of code, you will see:
Object of class B
Type checking:
isa (b,'A')
1
isa (b,'B')
1

How to call a task in another object in systemverilog?

There is a task T under object C, object B and C are in the same level under object A. How could object B call the task T under object C when timeout?
Thanks in advance.
Normal methods cannot be accessed outside its scope.
However if you have static method defined in, say class C. To access this method in class B, use scope resolution "::" operator with class name.
For e.g. to access static "display" method from class C to class B,
class B;
function void display();
C::display();
endfunction
endclass

How to call foo() function from class A

class A(object):
def foo(self):
print 'A'
class B(A):
def foo(self):
print 'B'
class C(B):
pass
c = C()
c.foo()
>>> B
I want to call the foo() method of class A. So how can I call this foo() method so that it will print 'A'
To directly answer your question, you could define C like this:
class C(B):
def foo(self):
A.foo(self)
However, a better question might be: why are you trying to do this? A class needing to access functions from the, erm, "grandparent" that were also defined in the parent class is a pretty good sign that your inheritance model is not ideal. For example: does C need anything from B; could it inherit directly from A?

implementing get/set property in a concrete class which inherits from an abstract class

I am trying to do the following in MATLAB:
define a super-class with property a:
classdef (Abstract) superClass
properties (Abstract = true)
a;
end
end
define a sub-class which inherits from super-class and implements a get function for a
classdef subClass < superClass
methods
function val = get.a(obj)
val = obj.a;
end
end
properties
a;
end
end
What I get is:
Error using subClass
Error: File: subClass.m Line: 3 Column: 28
Cannot specify a get function for property 'a' in class 'subClass', because that property is not defined by that class.
According to the documentation it suppose to be ok to define get/set on abstract properties.
Interesting, I got it to work by simply defining the properties block before the methods block in the subclass (the order seems to matter here):
classdef subClass < superClass
properties
a;
end
methods
function val = get.a(obj)
val = obj.a;
end
end
end
I think this is an issue with the MATLAB parser, perhaps you should file a bug report.

Classdef and properties

For OO programming we use classdef in matlab.
Could someone explain to me how could I access the super class ? What is the role of properties, are they like constructors in Java?
Use # to access the superclass. From Calling Superclass Methods on Subclass Objects:
classdef MySub < MySuperClass
methods
function obj = MySub(arg1,arg2,...)
obj = obj#MySuperClass(SuperClassArguments);
...
end % MySub
end % methods
end % classdef
Properties are like member variables. Properties – Storing Class Data.
To define a subclass you could look at the documentation
Single inheritance:
classdef classname < superclassname
For multiple inheritance:
classdef classname < super1 & super2
Properties are more like instance variables in Java.