Inheritance Polymorphism In Matlab - 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

Related

Upcasting in SystemVerilog

I'm trying to understand Upcasting and Downcasting in SystemVerilog.
Upcasting is casting to a supertype, while downcasting is casting to a subtype.
Upcasting is always allowed, but Downcasting involves a type check.
So I made a simple test code for upcasting and downcasting.
1.Test for upcasting.
class animal;
function void eat();
$display("eat");
endfunction
endclass
class dog extends animal;
function void bark();
$display("woof");
endfunction
endclass
class cat extends animal;
function void meow();
$display("meow");
endfunction
endclass
module test;
animal a1,a2;
dog d1, d2;
cat c1, c2;
initial begin
a1=new();
d1=new();
c1=new();
d1.eat();
c1.eat();
d1.bark();
c1.meow();
c1 = d1;
c1.bark(); //bark is not a class item.
d1 = c1;
d1.meow(); //meow is not a class item.
a1 = d1;
a1.bark(); //bark is not a class item.
a1 = c1;
a1.meow(); //meow is not a class item.
end
endmodule
In the class animal, dog and cat, I need to use bark and meow function from a derived dog and meow class. So I declared as the below But I got the compile error
c1 = d1;
c1.bark(); //bark is not a class item.
d1 = c1;
d1.meow(); //meow is not a class item.
a1 = d1;
a1.bark(); //bark is not a class item.
a1 = c1;
a1.meow(); //meow is not a class item.
As I know, the base class want/need to a properties from a derived class then we do upcasting. How do I get c1's bark(), d1's meow(), a1's bark(), a1.meow()?
If you comment out all the lines after c1 = d1;, you get a more helpful error message:
c1 = d1;
|
xmvlog: *E,TYCMPAT: assignment operator type check failed
(expecting datatype compatible with 'class $unit::cat' but found
'class $unit::dog' instead).
This gives you the reason why bark is not a class item: the assignment failed because c1 is not the same type as d1.
The VCS simulator behaves the same way, and produces a similar compile error.

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

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.

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.

How do I perform an import for the entire class in matlab?

I have a class that uses other classes from another package in multiple functions. In order to do this, I current have to import the package in each function:
classdef foo
properties
bar
end
methods
function self = foo()
foo.bar = 1;
end
function fun1(foo)
import pkg.FooClass;
val = pkg.FooClass(foo.bar);
end
function fun2(foo)
import pkg.FooClass;
val = FooClass.fun(foo.bar);
end
end
end
Is there a way to import packages for the entire class? I'm looking for something similar to other languages:
classdef foo
import pkg.FooClass;
properties
bar
end
methods
function self = foo()
foo.bar = 1;
end
function fun1(foo)
val = pkg.FooClass(foo.bar);
end
function fun2(foo)
val = FooClass.fun(foo.bar);
end
end
end
Unfortunately, the doc page says that:
The import function only affects the import list of the function
within which it is used.
So you will either have to specify the full qualified name everywhere, or do an import in each function.