Complete the code to define a subclass that inherits from a superclass.
classdef Dog < [1] methods function bark(obj) disp('Woof!') end end end
< symbol for inheritance.The subclass Dog inherits from the superclass Animal using the < symbol.
Complete the code to call the superclass constructor inside the subclass constructor.
classdef Cat < Animal
methods
function obj = Cat(name)
obj = obj@[1](name);
end
end
endobj.To call the superclass constructor, use obj = obj@Superclass(args); where Superclass is the name of the parent class.
Fix the error in the subclass method overriding the superclass method.
classdef Bird < Animal
methods
function sound(obj)
disp([1]);
end
end
enddisp.sound() inside itself causing recursion.The disp function requires a string input enclosed in quotes. So use 'Chirp!'.
Fill both blanks to create a subclass that adds a new method and calls a superclass method.
classdef Fish < Animal
methods
function swim(obj)
disp('Swimming');
end
function makeSound(obj)
obj.[1]();
disp([2]);
end
end
enddisp.The subclass calls the superclass method sound using obj.sound() and then displays 'Splash!'.
Fill all three blanks to override a superclass method and add extra behavior.
classdef Horse < Animal
methods
function sound(obj)
[1]@Animal(obj);
disp([2]);
obj.[3]();
end
function neigh(obj)
disp('Neigh!');
end
end
endThe subclass method sound calls the superclass sound method, displays 'Whinnying!', and then calls its own neigh method.
