call super class constructor in matlab

In Matlab, calling the superclass constructor from the derived class can be achieved using the super() method. Here's an example:

main.m
classdef MyClass < MySuperClass
   properties
      % Properties
   end
   
   methods
      % Methods including constructor
      function obj = MyClass(arg1, arg2)
         % Call the superclass constructor
         obj = obj@MySuperClass(arg1);  
         % Perform extra initialization
         obj.Variable2 = arg2;
      end
   end
end
342 chars
16 lines

Here, we defined a class MyClass inherited from MySuperClass. In the constructor of MyClass, we called the constructor of MySuperClass using obj@MySuperClass(arg1) syntax.

arg1 is passed to the constructor of the superclass MySuperClass.

If the superclass has no constructor, no super() method is required.

Note that in Matlab, each class must have a separate file with a name that matches the class name.

related categories

gistlibby LogSnag