Inheritance in MATLAB - Time & Space Complexity
When using inheritance in MATLAB, it is important to understand how it affects the speed of your program.
We want to see how the time to run code changes as the input or object size grows.
Analyze the time complexity of the following MATLAB class inheritance example.
classdef Animal
methods
function speak(obj)
disp('Animal sound')
end
end
end
classdef Dog < Animal
methods
function speak(obj)
disp('Bark')
end
end
end
% Create array of Dog objects and call speak
for i = 1:n
d = Dog();
d.speak();
end
This code defines a base class and a child class. It creates many Dog objects and calls a method on each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop creating and calling speak() on each Dog object.
- How many times: The loop runs
ntimes, once per Dog object.
Each new Dog object requires creating the object and calling its method once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 object creations and method calls |
| 100 | About 100 object creations and method calls |
| 1000 | About 1000 object creations and method calls |
Pattern observation: The work grows directly with the number of objects created.
Time Complexity: O(n)
This means the time to run grows in a straight line as you create more objects and call their methods.
[X] Wrong: "Inheritance makes the code slower exponentially because of extra layers."
[OK] Correct: Inheritance adds a small fixed overhead per method call, but the main time depends on how many objects and calls you make, not the inheritance depth.
Understanding how inheritance affects time helps you write clear and efficient object-oriented code, a useful skill in many programming tasks.
What if we changed the loop to call a method that itself calls another inherited method? How would the time complexity change?
Practice
Solution
Step 1: Understand inheritance concept
Inheritance means a subclass can use code from a parent class.Step 2: Identify what subclass gains
The subclass gets properties and methods from the parent class automatically.Final Answer:
Use properties and methods from a parent class -> Option BQuick Check:
Inheritance = reuse parent code [OK]
- Thinking inheritance creates variables automatically
- Confusing inheritance with running code without main
- Believing inheritance changes built-in MATLAB functions
Dog that inherits from superclass Animal in MATLAB?Solution
Step 1: Recall MATLAB inheritance syntax
MATLAB uses<symbol to indicate inheritance in classdef.Step 2: Match correct syntax
The correct syntax isclassdef Dog < Animal.Final Answer:
classdef Dog < Animal -> Option DQuick Check:
Inheritance uses < in MATLAB [OK]
< to inherit in MATLAB classdef [OK]- Using 'inherits' keyword instead of
< - Using ':' or 'extends' which are not MATLAB syntax
- Confusing MATLAB syntax with other languages
classdef Animal
methods
function sound(obj)
disp('Some sound')
end
end
end
classdef Dog < Animal
methods
function sound(obj)
disp('Bark')
end
end
end
myPet = Dog();
myPet.sound();What will be the output when running
myPet.sound()?Solution
Step 1: Understand method overriding in subclass
The subclassDogdefines its ownsoundmethod, which replaces the superclass method.Step 2: Determine which method runs
CallingmyPet.sound()runs theDogversion, printing 'Bark'.Final Answer:
Bark -> Option CQuick Check:
Subclass method overrides superclass method [OK]
- Expecting superclass method output
- Thinking both methods run
- Assuming error due to overriding
classdef Cat <
methods
function meow(obj)
disp('Meow')
end
end
endSolution
Step 1: Check inheritance syntax
The symbol<must be followed by the superclass name.Step 2: Identify missing superclass
Here, no superclass name is given after<, causing syntax error.Final Answer:
Missing superclass name after < -> Option AQuick Check:
Inheritance requires superclass name after < [OK]
- Leaving out superclass name after <
- Thinking method names cause error
- Assuming constructor is mandatory
Vehicle with a method move() that prints 'Moving'. You want to create a subclass Car that inherits Vehicle but adds a new method honk() that prints 'Beep!'. Which of the following is the correct MATLAB code for the subclass Car?Solution
Step 1: Use correct inheritance syntax
MATLAB uses<to inherit, soclassdef Car < Vehicleis correct.Step 2: Add new method in subclass
The methodhonkis added insidemethodsblock correctly.Final Answer:
classdef Car < Vehicle with honk method -> Option AQuick Check:
Subclass syntax + new method = classdef Car < Vehicle methods function honk(obj) disp('Beep!') end end end [OK]
< and add methods block for new methods [OK]- Using 'inherits', ':', or 'extends' instead of
< - Forgetting to add methods block
- Trying to override without correct syntax
Ready to prove it?
Pass the test and this pattern moves unknown
