Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Why classes need 'InferiorClasses' to determine which method

Date: 2023-07-31 14:57:53

Why classes need 'InferiorClasses' to determine which method to call when multiple classes have the same method

Document from here .I know how to use InferiorClasses.But I don't know why I need to use InferiorClasses?
 
For example,there is two classes,I don't use InferiorClasses! When I use classA's methods,just use "
 
obj_classA.MyMethod". When I use classB's methods,just use " obj_classB.MyMethod".
 
it's very flexible?So Why classes need 'InferiorClasses' to determine which method to call when multiple classes have the same method?
classdef  classA
       properties
           a;
       end
       methods
         function obj=classA(value)
             obj.a=value;
         end
         function MyMethod (obj,obj_classB)
             disp('The MyMethod of classA Called!');
         end
       end
end
 classdef %(InferiorClasses = {?classA}) classB 
        properties
           b;
        end
         methods
         function obj=classB(value)
             obj.b=value;
         end
         function MyMethod (obj,obj_classB)
             disp('The MyMethod of classB is Called!'); 
         end
         end
  end
  obj_classA = classA(3);
  obj_classB = classB(4);

Answer:

There are several different reasons. The first is that not all of our users are familiar with or comfortable using the object.method syntax. A large subset of our user base prefers function(inputs) syntax, and in that syntax there are situations where you don't want the left-most object to control which class's overload of a method to be called.
 
 
One concrete example is this: plot(ax, obj) where ax is an axes object and obj is an object whose class has a plot method. Which plot method should this call? The axes object is the left-most object, but we don't want it to have to know about every object that could be plotted in it. So many graphics classes will define axes as an InferiorClass, to ensure that plot call would call their plot method rather than the plot method of axes.
 
 
Another is that sometimes the order of the input arguments matters. If A and B are two classes that each overload the minus method, how can we use B's minus to compute A-B? B.minus(A) computes B-A which is not what I want.
 
But if A and B have the same priority, both A.minus(B) and A-B would call A's minus. In this case B would declare A as an InferiorClass to force A-B to call B's minus.

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com