Regarding the significance of abstract classes in Matlab, is my understanding correct?"
19 views (last 30 days)
Show older comments
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
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."
Answers (1)
Hitesh
on 29 Jan 2025 at 6:52
Hi fa wu,
Abstract classes in MATLAB are used to enforce a contract for subclasses, ensuring that certain methods are implemented. Kindly refer to the following example in MATLAB demonstrating the use of abstract classes to enforce method.
% Abstract class definition
classdef (Abstract) Pet
methods (Abstract)
eat(obj)
jump(obj)
end
end
% Dog class inheriting from Pet
classdef Dog < Pet
methods
function eat(obj)
disp('The dog is eating.');
end
function jump(obj)
disp('The dog jumps over the fence.');
end
end
end
% Cat class inheriting from Pet
classdef Cat < Pet
methods
function eat(obj)
disp('The cat is eating.');
end
function jump(obj)
disp('The cat jumps onto the roof.');
end
end
end
% Example usage
dog = Dog();
dog.eat(); % Outputs: The dog is eating.
dog.jump(); % Outputs: The dog jumps over the fence.
cat = Cat();
cat.eat(); % Outputs: The cat is eating.
cat.jump(); % Outputs: The cat jumps onto the roof.
Instances of "Dog" and "Cat" are created, and their methods are called. If you try to create a subclass that does not implement "eat" or "jump" function, MATLAB will throw an error, demonstrating the enforcement of method implementation.
This is how abstract classes in MATLAB ensure that all subclasses adhere to a specified interface, which is particularly useful in maintaining consistency and preventing errors in collaborative projects.
For more information regarding the "Abstract Class", kindly refer to the following MATLAB documentation:
0 Comments
See Also
Categories
Find more on Java Package Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!