Thread Subject: abstract class

Subject: abstract class

From: pp

Date: 22 Nov, 2009 14:47:47

Message: 1 of 4

Hello,


I wish to do a trick with abstract classes an inheritance. I create an
abstract class:

classdef compare
     methods (Abstract)
         lt(a,b)
         eq(a,b)
     end
      methods
         function r=ne(a,b)
             r=~eq(a,b);
         end
         function r=le(a,b)
             r=eq(a,b)||lt(a,b);
         end
         function r=gt(a,b)
             r=~lt(a,b)&&~eq(a,b);
         end
         function r=ge(a,b)
             r=~lt(a,b);
         end
     end
  end

and then I implement a concrete class:

classdef C < compare
     methods
         function r=lt(a,b)
         ...
         end
         function r=eq(a,b)
         ...
         end
     end
end

so that the other comparing function are inherited. Matlab is not
complaining when doing this, but using the debugger I see that the
methods in the superclass are not called. What am I doing wrong?

Regards

Subject: abstract class

From: Matt

Date: 22 Nov, 2009 16:34:07

Message: 2 of 4

PP <pp@pp.com> wrote in message <hebiuk$1go$1@news.eternal-september.org>...
> Hello,
>
>
> I wish to do a trick with abstract classes an inheritance. I create an
> abstract class:
>
> classdef compare
> methods (Abstract)
> lt(a,b)
> eq(a,b)
> end
> methods
> function r=ne(a,b)
> r=~eq(a,b);
> end
> function r=le(a,b)
> r=eq(a,b)||lt(a,b);
> end
> function r=gt(a,b)
> r=~lt(a,b)&&~eq(a,b);
> end
> function r=ge(a,b)
> r=~lt(a,b);
> end
> end
> end
>
> and then I implement a concrete class:
>
> classdef C < compare
> methods
> function r=lt(a,b)
> ...
> end
> function r=eq(a,b)
> ...
> end
> end
> end
>
> so that the other comparing function are inherited. Matlab is not
> complaining when doing this, but using the debugger I see that the
> methods in the superclass are not called. What am I doing wrong?

It works fine for me. What is happening instead, precisely?

One thing that might be playing a role here is that you are using short-circuited relational operators || and &&. This means for example that in the expression

 r=eq(a,b)||lt(a,b);

lt(a,b) will never be called if eq(a,b) evaluates true.

BTW, the short-circuited operators are almost certainly not what you want to use. It means your methods can only be used when eq(a,b) and lt(a,b) return scalars.

Subject: abstract class

From: pp

Date: 23 Nov, 2009 06:20:38

Message: 3 of 4

Matt schreef:
> PP <pp@pp.com> wrote in message <hebiuk$1go$1@news.eternal-september.org>...
>> Hello,
>>
>>
>> I wish to do a trick with abstract classes an inheritance. I create an
>> abstract class:
>>
>> classdef compare
>> methods (Abstract)
>> lt(a,b)
>> eq(a,b)
>> end
>> methods
>> function r=ne(a,b)
>> r=~eq(a,b);
>> end
>> function r=le(a,b)
>> r=eq(a,b)||lt(a,b);
>> end
>> function r=gt(a,b)
>> r=~lt(a,b)&&~eq(a,b);
>> end
>> function r=ge(a,b)
>> r=~lt(a,b);
>> end
>> end
>> end
>>
>> and then I implement a concrete class:
>>
>> classdef C < compare
>> methods
>> function r=lt(a,b)
>> ...
>> end
>> function r=eq(a,b)
>> ...
>> end
>> end
>> end
>>
>> so that the other comparing function are inherited. Matlab is not
>> complaining when doing this, but using the debugger I see that the
>> methods in the superclass are not called. What am I doing wrong?
>
> It works fine for me. What is happening instead, precisely?
>
> One thing that might be playing a role here is that you are using short-circuited relational operators || and &&. This means for example that in the expression
>
> r=eq(a,b)||lt(a,b);
>
> lt(a,b) will never be called if eq(a,b) evaluates true.
>
> BTW, the short-circuited operators are almost certainly not what you want to use. It means your methods can only be used when eq(a,b) and lt(a,b) return scalars.

Today it started working magically? In this instance scalars are
returned, but I made a comment in my code about the possible problem for
future users, thanks for pointing this out.

Regards

Subject: abstract class

From: Holger Hiebel

Date: 23 Nov, 2009 07:50:03

Message: 4 of 4

PP <pp@pp.com> wrote in message <4B0A29B6.7070608@pp.com>...
> Matt schreef:
> > PP <pp@pp.com> wrote in message <hebiuk$1go$1@news.eternal-september.org>...
> >> Hello,
> >>
> >>
> >> I wish to do a trick with abstract classes an inheritance. I create an
> >> abstract class:
<snip>
> Today it started working magically?
<snip>

In some cases (e.g. files on Network drives, class instances in workspace...) Matlab uses old definitions and files from the Matlab filecache - maybe this was the case. Restarting the environment forces Matlab to use the newest files.

BR,
Holger

Tags for this Thread

Everyone's Tags:

oop

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
oop Matt J 22 Nov, 2009 11:36:00
rssFeed for this Thread

Contact us at files@mathworks.com