Regarding the significance of abstract classes in Matlab, is my understanding correct?"

5 views (last 30 days)
In C++, runtime polymorphism requires the presence of abstract classes or interfaces.
However, in Matlab, achieving polymorphism doesn't necessitate abstract classes; it can be directly implemented.It seems that abstract classes/interfaces in Matlab are not as indispensable as they are in C++.I speculate that the 'primary purpose' of abstract classes in Matlab is for collaborative work among team members.
For example, if the team leader does not specify the abstract class 'Pet,' the member writing the 'Dog' class may not implement the 'eat' method, and the member writing the 'Cat' class may not have a 'jump' method. However, if the team leader defines an abstract class 'Pet' and declares the 'eat' and 'jump' methods within it, specifying that the 'Dog' and 'Cat' classes must inherit from 'Pet,' the team members writing the 'Dog' and 'Cat' classes must then implement the 'eat' and 'jump' methods. Otherwise, the code will result in an error.Other than that, abstract classes in Matlab serve no other purpose
----------------Could you please confirm if my above speculation is correct?
  1 Comment
Matt J
Matt J on 25 Nov 2023
Edited: Matt J on 25 Nov 2023
In C++, runtime polymorphism requires the presence of abstract classes or interfaces.
I'm not sure that's true. ChatGPT certainly doesn't think so (not to say that that's gospel):
"Polymorphism relies on the use of virtual functions, and an abstract class is one way to ensure that these functions are declared in the base class and overridden in derived classes. However, you can achieve polymorphism in C++ without an abstract class by using virtual functions in a non-abstract base class and overriding them in derived classes. The key is to use pointers or references of the base class type to achieve dynamic dispatch at runtime."

Sign in to comment.

Answers (0)

Categories

Find more on Java Package Integration in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!