Bird
Raised Fist0
MATLABdata~20 mins

Inheritance in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method overriding in MATLAB inheritance

Consider the following MATLAB classes and script. What is the output when the script runs?

MATLAB
classdef Animal
    methods
        function sound(obj)
            disp('Some generic animal sound')
        end
    end
end

classdef Dog < Animal
    methods
        function sound(obj)
            disp('Bark')
        end
    end
end

% Script
myPet = Dog();
myPet.sound();
AError: Method sound not found
BBark
CSome generic animal sound
DNo output
Attempts:
2 left
💡 Hint

Think about which class's sound method is called when you create a Dog object.

❓ Predict Output
intermediate
2:00remaining
Output of calling superclass method explicitly

What is the output of this MATLAB code?

MATLAB
classdef Vehicle
    methods
        function start(obj)
            disp('Vehicle started')
        end
    end
end

classdef Car < Vehicle
    methods
        function start(obj)
            disp('Car engine warming up')
            start@Vehicle(obj);
        end
    end
end

myCar = Car();
myCar.start();
ACar engine warming up
B
Vehicle started
Car engine warming up
C
Car engine warming up
Vehicle started
DError: Invalid syntax for superclass method call
Attempts:
2 left
💡 Hint

Look at how the superclass method is called inside the subclass method.

🔧 Debug
advanced
2:00remaining
Identify the error in MATLAB inheritance code

What error does this MATLAB code produce when run?

MATLAB
classdef Parent
    properties
        Name
    end
    methods
        function obj = Parent(name)
            obj.Name = name;
        end
    end
end

classdef Child < Parent
    methods
        function obj = Child(name)
            obj.Name = name;
        end
    end
end

c = Child('Alice');
AError: No constructor for superclass Parent called in Child constructor
BNo error, object created successfully
CError: Property 'Name' is not accessible
DError: Syntax error in class definition
Attempts:
2 left
💡 Hint

Think about how constructors work in inheritance in MATLAB.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a subclass constructor calling superclass constructor?

Which of the following MATLAB subclass constructors correctly calls the superclass constructor?

A
function obj = Child(name)
    obj = obj.Parent(name);
end
B
function obj = Child(name)
    obj.Name = name;
    Parent(name);
end
C
function obj = Child(name)
    obj = Parent(name);
end
D
function obj = Child(name)
    obj = obj@Parent(name);
end
Attempts:
2 left
💡 Hint

Look for the correct syntax to call superclass constructor in MATLAB subclass constructor.

🚀 Application
expert
2:00remaining
Determine the number of methods available to subclass object

Given these MATLAB classes, how many methods does an object of class Manager have access to?

MATLAB
classdef Employee
    methods
        function work(obj)
            disp('Working')
        end
        function report(obj)
            disp('Reporting')
        end
    end
end

classdef Manager < Employee
    methods
        function manage(obj)
            disp('Managing')
        end
    end
end

m = Manager();
A3
B1
C2
D4
Attempts:
2 left
💡 Hint

Count all methods defined in Manager and its superclass Employee.

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

  1. Step 1: Understand inheritance concept

    Inheritance means a subclass can use code from a parent class.
  2. Step 2: Identify what subclass gains

    The subclass gets properties and methods from the parent class automatically.
  3. Final Answer:

    Use properties and methods from a parent class -> Option B
  4. Quick Check:

    Inheritance = reuse parent code [OK]
Hint: Inheritance means reuse parent class code [OK]
Common Mistakes:
  • Thinking inheritance creates variables automatically
  • Confusing inheritance with running code without main
  • Believing inheritance changes built-in MATLAB functions
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

  1. Step 1: Recall MATLAB inheritance syntax

    MATLAB uses < symbol to indicate inheritance in classdef.
  2. Step 2: Match correct syntax

    The correct syntax is classdef Dog < Animal.
  3. Final Answer:

    classdef Dog < Animal -> Option D
  4. 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

  1. Step 1: Understand method overriding in subclass

    The subclass Dog defines its own sound method, which replaces the superclass method.
  2. Step 2: Determine which method runs

    Calling myPet.sound() runs the Dog version, printing 'Bark'.
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Subclass method overrides superclass method [OK]
Hint: Subclass method overrides superclass method [OK]
Common Mistakes:
  • Expecting superclass method output
  • Thinking both methods run
  • Assuming error due to overriding
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

  1. Step 1: Check inheritance syntax

    The symbol < must be followed by the superclass name.
  2. Step 2: Identify missing superclass

    Here, no superclass name is given after <, causing syntax error.
  3. Final Answer:

    Missing superclass name after < -> Option A
  4. 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

  1. Step 1: Use correct inheritance syntax

    MATLAB uses < to inherit, so classdef Car < Vehicle is correct.
  2. Step 2: Add new method in subclass

    The method honk is added inside methods block correctly.
  3. Final Answer:

    classdef Car < Vehicle with honk method -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard