Study and learn Interview MCQ Questions and Answers on Method Overriding in Java. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Method Overriding before reading these objective questions.
Yes. It is not mandatory. If the inheriting method serves the purpose, use it directly. Otherwise, write your custom code in the overridden method.
All the above.
TRUE
class Bus { void seatingCapacity() { System.out.println("Superclass Seats=32"); } } class ElectricBus extends Bus { void seatingCapacity() { System.out.println("Subclass Seats=20"); } void showInfo() { seatingCapacity(); this.seatingCapacity(); } } public class MethodOverriding1 { public static void main(String[] args) { ElectricBus eb = new ElectricBus(); eb.showInfo(); } }
Subclass Seats=20 Superclass Seats=32
Superclass Seats=32 Subclass Seats=20
Superclass Seats=32 Superclass Seats=32
Subclass Seats=20 Subclass Seats=20
Using the keyword "this" calls the local method of the class but not the method of a superclass.
class Car { void showTransmission() { System.out.println("Transmission Manual"); } } class ElectricCar extends Car { void showTransmission() { System.out.println("Transmission AMT"); } void showInfo() { this.showTransmission(); super.showTransmission(); } } public class MethodOverriding2 { public static void main(String[] args) { ElectricCar ec = new ElectricCar(); ec.showInfo(); } }
Transmission AMT Transmission Manual
Transmission Manual Transmission AMT
Transmission Manual Transmission Manual
Transmission AMT Transmission AMT
The keyword "super" calls the method of a Superclass.
void superclassMethod(int a, float b){ } void subclassMethod(int a, float b) { }
void superclassMethod(){ } void subclassMethod(){ }
int superclassMethod(int a, float b){ } void subclassMethod(int a, float b) { }
The return types are different. So, it is not a successful method override.
The method overriding is implemented to give preference to the method of a Subclass.
If a method override fails, the JVM may call the method of either a Superclass or Subclass. It depends on the parameters passed in the method call.
class Cat { int jumpingHeight(int weight) { System.out.println(10); return 10; } } class WildCat extends Cat { void jumpingHeight(int weight) { System.out.println("20"); } } public class MethodOverriding3 { public static void main(String[] args) { WildCat wc = new WildCat(); wc.jumpingHeight(30); } }
If the argument list is the same, the return types can not be the incomptible-types. So, the compiler reports an error "The return type is incompatible with Cat.jumpingHeight(int)".
class Sparrow{ } class BigSparrow extends Sparrow { } class Cat2 { Sparrow jumpingHeight(int weight) { System.out.println(40); return new Sparrow(); } } class WildCat2 extends Cat2 { BigSparrow jumpingHeight(int weight) { System.out.println("50"); return new BigSparrow(); } } public class MethodOverriding4 { public static void main(String[] args) { WildCat2 wc = new WildCat2(); wc.jumpingHeight(80); } }
It is perfectly alright to use a subclass type return type when overriding a method in Java. BigSparrow is the subclass of Sparrow. Always, the overriding method will be called.
class Steel { void setGrade(int g) { System.out.print(",GRADE="+g); } } class CarbonSteel extends Steel { void setGrade(char grade) { System.out.print(",Grade="+grade); } } public class MethodOverriding5 { public static void main(String[] args) { Steel s = new CarbonSteel(); s.setGrade(5); s.setGrade('A'); } }
As the superclass reference "s" is used, it calls the methods of the superclass only. As the method signatures of the "setGrade" method are different with different argument types, it is not a successful override. It is an overloading of the superclass's method.
class Wood { void setQuality(int q) { System.out.print(",QUALITY="+q); } } class PlyWood extends Wood { void setQuality(char qual) { System.out.print(",quality="+qual); } } public class MethodOverriding6 { public static void main(String[] args) { PlyWood pw = new PlyWood(); pw.setQuality(10); pw.setQuality('B'); //ASCII of B=66 } }
The method "setQuality" is not overridden successfully as the argument types are different. The subclass type reference can call a method of superclass and subclass.
class Amplifier { void addGain(int a) { System.out.println((a + 10)+"dB"); } } class DigitalAmplifier extends Amplifier { void addGain(int a) { super.addGain(a+5); } } public class MethodOverriding7 { public static void main(String[] args) { DigitalAmplifier da = new DigitalAmplifier(); da.addGain(12); } }
The subclass DigitalAmplifier successfully overrides the method of the superclass "Amplifier". In the subclass's method, an extra gain of 5 is added and passed to the superclass's method. So, it becomes 27(12+5+10).
True. Only subclass methods can override the methods of a superclass.
Yes. The to be called at runtime is decided at runtime based on successful or failed Overriding.
An Overriding method belongs to the Subclass and the Overridden method belongs to the Superclass.
The access modifier can be less restrictive.
protected void show() { } . . public void show() { } //public is less restrictive than private.
void show() throws IOException{ } . . void show() throws FileNotFoundException{ }
void show() throws IOException{ } . . void show() throws ArithmeticException{ }
void show() throws ArithmeticException{ } . . void show() throws IllegalFormatException{ }
The exception thrown by the subclass's method can be a subclass type of the exception thrown by the superclass's method. The superclass of FileNotFoundException is IOException only.