Inheritance lets one class use properties and methods from another class. It helps avoid repeating code and makes programs easier to manage.
When you want to create a new class that is a special version of an existing class.
When you want to share common features between different classes without rewriting code.
When you want to organize related classes in a clear hierarchy.
When you want to add new features to an existing class without changing it.
When you want to reuse tested code safely in new classes.
Syntax
MATLAB
classdef SubClassName < SuperClassName
% class body
end
The < symbol means 'inherits from'.
The subclass gets all properties and methods of the superclass automatically.
Examples
This is a simple superclass named Animal with one method.
MATLAB
classdef Animal
methods
function speak(obj)
disp('Animal sound')
end
end
end
Dog inherits from Animal and changes the speak method.
MATLAB
classdef Dog < Animal
methods
function speak(obj)
disp('Woof!')
end
end
end
Create a Dog object and call its speak method, which shows 'Woof!'.
MATLAB
d = Dog();
d.speak();
Sample Program
This program shows how Dog inherits from Animal. The Dog class changes the speak method to say 'Woof!'.
MATLAB
classdef Animal
methods
function speak(obj)
disp('Animal sound')
end
end
end
classdef Dog < Animal
methods
function speak(obj)
disp('Woof!')
end
end
end
% Create objects
myAnimal = Animal();
myDog = Dog();
% Call methods
myAnimal.speak();
myDog.speak();
OutputSuccess
Important Notes
If a subclass has a method with the same name as the superclass, it replaces it (called overriding).
You can still call the superclass method inside the subclass using methodName@SuperClassName(obj).
Inheritance helps keep code clean and easy to update.
Summary
Inheritance lets a class use code from another class.
Use < to inherit in MATLAB class definitions.
Subclasses can add or change methods from the superclass.
Practice
(1/5)
1. What does inheritance allow a subclass to do in MATLAB?
easy
A. Change MATLAB's built-in functions
B. Use properties and methods from a parent class
C. Run code without a main function
D. Create variables without declaring them
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 B
Quick Check:
Inheritance = reuse parent code [OK]
Hint: Inheritance means reuse parent class code [OK]
2. Which of the following is the correct way to declare a subclass Dog that inherits from superclass Animal in MATLAB?
easy
A. classdef Dog inherits Animal
B. classdef Dog : Animal
C. classdef Dog extends Animal
D. classdef Dog < Animal
Solution
Step 1: Recall MATLAB inheritance syntax
MATLAB uses < symbol to indicate inheritance in classdef.
Step 2: Match correct syntax
The correct syntax is classdef Dog < Animal.
Final Answer:
classdef Dog < Animal -> Option D
Quick Check:
Inheritance uses < in MATLAB [OK]
Hint: Use < to inherit in MATLAB classdef [OK]
Common Mistakes:
Using 'inherits' keyword instead of <
Using ':' or 'extends' which are not MATLAB syntax
Confusing MATLAB syntax with other languages
3. Consider the following MATLAB code:
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()?
medium
A. Error: Method not found
B. Some sound
C. Bark
D. No output
Solution
Step 1: Understand method overriding in subclass
The subclass Dog defines its own sound method, which replaces the superclass method.
Step 2: Determine which method runs
Calling myPet.sound() runs the Dog version, printing 'Bark'.
4. What is wrong with this MATLAB subclass definition?
classdef Cat <
methods
function meow(obj)
disp('Meow')
end
end
end
medium
A. Missing superclass name after <
B. Method name cannot be 'meow'
C. Classdef keyword is misspelled
D. No constructor defined
Solution
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 A
Quick Check:
Inheritance requires superclass name after < [OK]
Hint: Always specify superclass name after < [OK]
Common Mistakes:
Leaving out superclass name after <
Thinking method names cause error
Assuming constructor is mandatory
5. You have a superclass 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?
hard
A. classdef Car < Vehicle
methods
function honk(obj)
disp('Beep!')
end
end
end
B. classdef Car inherits Vehicle
methods
function honk(obj)
disp('Beep!')
end
end
end
C. classdef Car : Vehicle
methods
function honk(obj)
disp('Beep!')
end
end
end
D. classdef Car extends Vehicle
methods
function honk(obj)
disp('Beep!')
end
end
end
Solution
Step 1: Use correct inheritance syntax
MATLAB uses < to inherit, so classdef Car < Vehicle is correct.
Step 2: Add new method in subclass
The method honk is added inside methods block correctly.
Final Answer:
classdef Car < Vehicle with honk method -> Option A
Quick Check:
Subclass syntax + new method = classdef Car < Vehicle
methods
function honk(obj)
disp('Beep!')
end
end
end [OK]
Hint: Use < and add methods block for new methods [OK]
Common Mistakes:
Using 'inherits', ':', or 'extends' instead of <
Forgetting to add methods block
Trying to override without correct syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.