Method Overriding in JAVA Interview MCQ Questions and Answers
Table of Contents
Study and learn Interview MCQ Questions and Answers on Method Overriding in JAVA . Attend job interviews easily with these Multiple Choice Questions
Method Overriding in JAVA
Java Inheritance allows inheriting the methods and properties of a Superclass into a Subclass. And Method Overriding is nothing but writing a method in a Subclass with the same method signature of the Superclass so that the Java Runtime invokes the method in the subclass. Let us know more with examples.
Method overloading and method overriding in java
Difference between Method Overloading and Method Overriding
- Java Method Overloading works in the Same Class whereas Method Overriding requires at least two classes (Superclass and Subclass).
- Method Overloading allows reusing the same method name for a different combination of parameters whereas Method Overriding forces the Java Runtime to choose the method in the Subclass to the method of Superclass.
Java Method Signature is the combination of three things namely Return-Type, Method-Name and Parameter-List.
Rules to successfully Override
- Overriding-Method Name in the Superclass and Subclass should be the same.
- The Parameter list of the Overriding-Method in the Superclass and Subclass should be the same.
- The Return-type of Overriding method in the Subclass can be the same as the return-type of superclass’s method. For example, use int and int return-types in both the methods.
- The Return-type of Overriding method in the Subclass can be a Subclass-type of return-type of superclass’s method. For example, List and ArrayList are return types of Superclass method and Subclass method respectively.
- Access modifier used by the Overridden method of the Subclass should be Less restrictive or same. For example, use protected and public modifiers for methods of Superclass and Subclass respectively.
- Exceptions thrown by the Overridden method of the Subclass should be either same or subtype. Even the Subclass method can omit to throw any exception at all in the process of Overriding a Superclass method.
So, you can maintain the Same method signature for both the methods in the Superclass and Subclass with minor changes to the return type for a successful Method Override.
Method Overriding in JAVA Example
Example 1: Method Overriding
Method Overriding in JAVA Example
class Vehicle { void showInfo() { System.out.println("Generic Vehicle"); } void dummy() { System.out.println("Vehicle Dummy"); } } class Car extends Vehicle { void showInfo() //Overridden method { System.out.println("CAR BENZ"); } } public class MethodOverridingExample { public static void main(String[] args) { Vehicle veh = new Vehicle(); //Superclass reference - superclass object //Superclass reference pointing subclass object Vehicle veh2 = new Car(); Car car = new Car(); //subclass reference subclass object veh.showInfo(); veh2.showInfo(); car.showInfo(); veh.dummy(); veh2.dummy(); car.dummy(); } } //OUTPUT Generic Vehicle CAR BENZ CAR BENZ Vehicle Dummy Vehicle Dummy Vehicle Dummy
In the above example, we have a Vehicle-Superclass and a Car-Subclass. Car Subclass maintained the same method signature of the Vehicle Superclass. Thus, showInfo() method has been overridden. If a method of Superclass is not overridden, it can be called simply by the Car-Subclass object as if it is part of the Car-Subclass. Even if the method dummy() is overridden, it can be called by a Car object. The same can observed seeing the output of the method dummy() by a Vehicle object and a Car object.
Note: If a method or variable is present only in the Subclass, then it can not called or accessed by a Superclass reference. Only Overridden methods and variables of a subclass can accessed through a Superclass reference.
Example 2: Method Overriding
Method Overriding Example
class WaterMelonRound { //Superclass defines general or generic behaviour void show() { System.out.println("Round Watermelon - Superclass"); } } class WaterMelonSquare { //Subclass defines its own version of method void show() { System.out.println("Square Watermelon - Subclass"); } } public class MethodOverridingExample2 { public static void main(String[] args) { WaterMelonRound round = new WaterMelonRound(); round.show(); WaterMelonSquare square = new WaterMelonSquare(); square.show(); } } //OUTPUT Round Watermelon - Superclass Square Watermelon - Subclass
In the above example, we have a Superclass WaterMelonRound and a Subclass WaterMelonSquare. In the process of inheriting the properties of the superclass, a subclass can redefine or define new behaviour for the inherited methods through Overriding. The Overridden method show() in the subclass is an example of that.
[WpProQuiz 166]
Method Overriding in JAVA Interview MCQ
1) What is method overriding in Java?
A) Writing a method in a subclass with the same name of superclass’s method
B) Mentioning the same return type of the method of the superclass
C) The argument list in the method of subclass and the method of superclass should be the same
D) All the above
Answer [=] D
2) Method Overriding is useful to add extra functionality or code to a method of subclass with the same name as the inherited method. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
3) It is not mandatory to override all or a few methods of the Superclass. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
Yes. It is not mandatory. If the inheriting method serves the purpose, use it directly. Otherwise, write your custom code in the overridden method.
4) Why should a method be overridden in Java instead of writing a method with a different name?
A) Large projects heavily depend on inheritance
B) The code-base refers to the same method with the same method signature in different classes of the project
C) It is not possible to change the method calling code at all occurrences of the project. It may break the whole project.
D) All the above.
Answer [=] D
Explanation:
All the above.
5) The Method-Overloading and Method-Overriding are not the same. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
TRUE
6) What is the output of the below Java program with Method Overriding?
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(); } }
A)
Subclass Seats=20 Superclass Seats=32
B)
Superclass Seats=32 Subclass Seats=20
C)
Superclass Seats=32 Superclass Seats=32
D)
Subclass Seats=20 Subclass Seats=20
Answer [=] D
Explanation:
Using the keyword “this” calls the local method of the class but not the method of a superclass.
7) What is the output of the below Java program with Method Overriding and SUPER keyword?
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(); } }
A)
Transmission AMT Transmission Manual
B)
Transmission Manual Transmission AMT
C)
Transmission Manual Transmission Manual
D)
Transmission AMT Transmission AMT
Answer [=] A
Explanation:
The keyword “super” calls the method of a Superclass.
8) Identify INVALID Java Method Overriding in the below code snippets? Follow the notation “superclassMethod” and “subclassMethod”.
A)
void superclassMethod(int a, float b){ } void subclassMethod(int a, float b) { }
B)
void superclassMethod(){ } void subclassMethod(){ }
C)
int superclassMethod(int a, float b){ } void subclassMethod(int a, float b) { }
D) None
Answer [=] C
Explanation:
The return types are different. So, it is not a successful method override.
9) A successful Method Overriding calls the method of ___ in Java.
A) Superclass
B) Subclass
Answer [=] B
Explanation:
The method overriding is implemented to give preference to the method of a Subclass.
10) A failed method overriding calls the method of a ___ in Java.
A) Superclass
B) Subclass
C) Superclass or Subclass
D) None
Answer [=] C
Explanation:
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.
11) What is the output of the below Java program with method overriding?
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); } }
A) 10
B) 20
C) 30
D) Compiler error
Answer [=] D
Explanation:
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)”.
12) What is the output of the below Java program with Method Overriding?
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); } }
A) 40
B) 50
C) 80
D) Compiler error
Answer [=] B
Explanation:
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.
13) What is the output of the below Java program with method overriding?
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'); } }
A) ,GRADE=5,GRADE=A
B) ,GRADE=5,GRADE=65
C) ,Grade=5,Grade=65
D) Compiler error
Answer [=] B
Explanation:
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.
14) What is the output of the below Java program with method overriding?
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 } }
A) ,QUALITY=10,quality=B
B) ,QUALITY=10,quality=65
C) ,quality=10,quality=B
D) Compiler error
Answer [=] A
Explanation:
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.
15) What is the output of the below Java program with method overriding?
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); } }
A) 22dB
B) 27dB
C) 22dB 27dB
D) Compiler error
Answer [=] B
Explanation:
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).
16) If the method signature of a Superclass and the method signature of a Subclass are the same, then the subclass method is said to be _____ the superclass’s method.
A) Overriding
B) Overloading
Answer [=] A
17) A method of a Superclass can not override the method of the Subclass. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
True. Only subclass methods can override the methods of a superclass.
18) Method overriding increases the burden on the JVM in terms of runtime checks and resolution. State TRUE or FALSE.
A) FALSE
B) TRUE
Answer [=] B
Explanation:
Yes. The to be called at runtime is decided at runtime based on successful or failed Overriding.
19) What are the advantages of Method Overriding in Java?
A) A subclass can add extra functionality to the overriding method.
B) A subclass can call both the overridden method and overriding method.
C) It supports polymorphism. A superclass reference can be used to call the common method of all subclasses.
D) All the above
Answer [=] D
20) An Overridden method is the method of ____ class and the overriding method is the method of ___ class.
A) super, sub
B) sub, super
C) super, super
D) sub, sub
Answer [=] A
Explanation:
An Overriding method belongs to the Subclass and the Overridden method belongs to the Superclass.
21) To successfully override a superclass method in Java, the access modifier of the method of the subclass can be ___ restrictive.
A) Less
B) More
C) Less or Same
D) None
Answer [=] C
Explanation:
The access modifier can be less restrictive.
protected void show() { } . . public void show() { } //public is less restrictive than private.
22) Which is the correct way of overriding a method throwing exceptions in Java?
A)
void show() throws IOException{ } . . void show() throws FileNotFoundException{ }
B)
void show() throws IOException{ } . . void show() throws ArithmeticException{ }
C)
void show() throws ArithmeticException{ } . . void show() throws IllegalFormatException{ }
D) None
Answer [=] A
Explanation:
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.
For More Free online Quizzes of JAVA PROGRAMMING Test online Click here