In Dart, mastering inheritance and polymorphism is crucial for object-oriented programming. This chapter explores extending classes, method overriding, the @override annotation, and the use of the super keyword.
Chapter: Inheritance and Polymorphism in Dart – MCQs
1. Extending Classes
Which keyword is used to extend a class in Dart?
a) extend
b) inherits
c) extends
d) super
What happens when a class is extended in Dart?
a) The child class inherits the methods and properties of the parent class
b) The child class replaces the parent class
c) The parent class becomes abstract
d) The child class cannot have any properties
What is the primary purpose of extending a class in Dart?
a) To create an instance of the parent class
b) To allow the child class to inherit the parent’s behaviors and attributes
c) To prevent the child class from modifying the parent class
d) To make the parent class abstract
How do you call a constructor of the parent class from a subclass in Dart?
a) Using super()
b) Using parent()
c) Using call()
d) Using extends()
Which of the following code is used to extend the class Vehicle in Dart?
a) class Car implements Vehicle {}
b) class Car extends Vehicle {}
c) class Car inherits Vehicle {}
d) class Car create Vehicle {}
Can a class in Dart extend multiple classes?
a) Yes, Dart supports multiple inheritance
b) No, Dart does not support multiple inheritance
c) Only interfaces can be extended multiple times
d) Yes, but only with abstract classes
What happens if a child class doesn’t call the constructor of the parent class?
a) The program will throw an error
b) The parent’s constructor is automatically invoked
c) The child class will not be able to access the parent class’s methods
d) The child class must explicitly call the constructor
2. Method Overriding
What is method overriding in Dart?
a) Defining a new method in the child class with the same name as the parent
b) Calling a parent class method in the child class
c) Changing the type of method in the child class
d) Renaming a method in the parent class
How can you override a method in Dart?
a) By defining a method with the same name in the child class
b) By using the super keyword
c) By using the @override annotation
d) By changing the return type of the parent method
Which of the following is true about method overriding in Dart?
a) It can only be done with abstract methods
b) It changes the implementation of a method in the parent class
c) It is used to provide a new implementation for a method in a subclass
d) It calls the parent method automatically
Can method overriding be done with static methods in Dart?
a) Yes, static methods can be overridden
b) No, static methods cannot be overridden
c) Static methods in the child class hide the static methods in the parent class
d) Static methods in the child class must be declared abstract
Which of the following is an example of method overriding in Dart?
a) class Car { void start() {} }
b) class Car extends Vehicle { @override void start() {} }
c) class Car implements Vehicle { void start() {} }
d) class Car { void start() { super.start(); } }
What happens if you do not use the @override annotation in Dart when overriding a method?
a) Dart will throw an error
b) Dart will not allow method overriding
c) The method will still be overridden but without verification
d) The parent class method will be called automatically
3. The @override Annotation
What is the purpose of the @override annotation in Dart?
a) To indicate that a method is being overridden from the parent class
b) To change the method’s return type
c) To prevent inheritance
d) To make a method static
Which of the following is true about the @override annotation in Dart?
a) It is mandatory for method overriding
b) It is not required, but it improves code readability and ensures correctness
c) It makes the method abstract
d) It is used to call a parent method
How does Dart treat method overriding without using the @override annotation?
a) It will produce an error
b) It will compile and run normally
c) It will cause a performance issue
d) It will result in the method being ignored
In which situation should you use the @override annotation?
a) To indicate that a class is overriding a method
b) To change the return type of a method
c) To prevent a method from being inherited
d) To provide a new implementation for a method inherited from the parent class
What happens if you override a method without using the @override annotation in Dart?
a) The method is not overridden
b) The Dart compiler will not verify the override
c) It will cause an error
d) It will throw a runtime exception
Can the @override annotation be used for variables in Dart?
a) Yes, to override a variable in the child class
b) No, @override is only for methods
c) Yes, it can be used for both methods and variables
d) Yes, but only for final variables
4. The super Keyword
What is the super keyword used for in Dart?
a) To access methods and properties from the parent class
b) To invoke the parent class’s constructor
c) To define static variables
d) Both a and b
How do you use the super keyword in Dart to access the parent class constructor?
a) super();
b) super.create();
c) parent.constructor();
d) super.new();
What happens if you call super() in the constructor of a subclass?
a) It invokes the constructor of the parent class
b) It initializes a variable in the parent class
c) It overrides a method from the parent class
d) It is ignored
How do you call a method from the parent class in Dart?
a) super.methodName();
b) super.methodName;
c) call.superMethod();
d) parent.methodName();
Which of the following statements is true regarding the use of super in Dart?
a) You can call a method of the parent class using super
b) super is used to call a method from the child class
c) super can be used to access the variables in the child class
d) super prevents the inheritance of parent methods
Can super be used to refer to an overridden method in Dart?
a) Yes, it allows calling the parent’s method
b) No, only the overridden method is executed
c) It will result in an error
d) It calls the subclass method instead
How does super relate to method chaining in Dart?
a) It enables calling a method from the superclass while chaining methods
b) It only works for constructors
c) It breaks the method chaining process
d) It cannot be used for method chaining
What will happen if super is used without calling a constructor in Dart?
a) It will result in an error
b) It will call the default constructor
c) It will invoke a method from the parent class
d) It will skip initialization
Can the super keyword be used inside static methods in Dart?
a) Yes, to access static methods in the parent class
b) No, super cannot be used in static methods
c) Yes, but only to access instance methods
d) Yes, but only to call constructors
Can you access private members of the parent class using super in Dart?
a) Yes, if the parent class is in the same library
b) No, private members are inaccessible via super
c) Yes, always
d) Yes, if the subclass is part of the same package
In which scenario would you need to use the super keyword?
a) When invoking methods or constructors of the parent class
b) When overriding methods in the child class
c) When creating instances of the parent class
d) When accessing variables in the child class
Answers
Qno
Answer
1
c) extends
2
a) The child class inherits the methods and properties of the parent class
3
b) To allow the child class to inherit the parent’s behaviors and attributes
4
a) Using super()
5
b) class Car extends Vehicle {}
6
b) No, Dart does not support multiple inheritance
7
b) The parent’s constructor is automatically invoked
8
a) Defining a new method in the child class with the same name as the parent
9
a) By defining a method with the same name in the child class
10
c) It is used to provide a new implementation for a method in a subclass
11
b) No, static methods cannot be overridden
12
b) class Car extends Vehicle { @override void start() {} }
13
c) The method will still be overridden but without verification
14
a) To indicate that a method is being overridden from the parent class
15
b) It is not required, but it improves code readability and ensures correctness
16
b) It will compile and run normally
17
d) To provide a new implementation for a method inherited from the parent class
18
b) Dart compiler will not verify the override
19
b) No, @override is only for methods
20
d) Both a and b
21
a) super();
22
a) It invokes the constructor of the parent class
23
a) super.methodName();
24
a) You can call a method of the parent class using super
25
a) Yes, it allows calling the parent’s method
26
a) It enables calling a method from the superclass while chaining methods
27
a) It will result in an error
28
b) No, super cannot be used in static methods
29
b) No, private members are inaccessible via super
30
a) When invoking methods or constructors of the parent class