Scientech Easy 15 Top Java Method Overriding Interview Programs for Practice | Scientech Easy

Friday, April 19, 2019

15 Top Java Method Overriding Interview Programs for Practice

In this tutorial, you will get the most important interview programs for practice based on method overriding concept. Here, you can solve the interview programs based on concepts of method overriding, covariant return type,  exceptional handling with method overriding, the effect of access modifiers on method overriding. All the programs are the most important for Java beginners and 2-3 years of experience.
If you are able to find out the output of more than 10 programs in this tutorial, you can definitely solve any questions related to method overriding in any technical test or interview.
Method overriding interview programs
1. Is the below code successfully overridden?
    package overridingInterviewPrograms; public class A { private void m1(){ System.out.println("m1-A"); } } public class B extends A{ private void m1(){ System.out.println("m1-B"); } public static void main(String[] args) { B b=new B(); b.m1(); } }
Output: No.
Key points:
The overriding concept is not applicable to the private method. Parent class private method is not visible in the child class. Keep in mind. Based on our requirement, we can define exactly the same private method in the child class. The code is valid but not overriding.
2. Will the below code compile successfully?
    package overridingInterviewPrograms; public class A { public final void m1(){ System.out.println("m1-A"); } } public class B extends A{ public void m1(){ System.out.println("m1-B"); } public static void main(String[] args) { B b=new B(); b.m1(); } }
Output: No, Compile-time error.
Key points:
If the parent class method is declared as final, you cannot override it in the child class. If you are trying to override it, you will get the compile-time error-"Overridden method is final". 

3. Will the below code compile successfully? If yes, what will be the output?
    package overridingInterviewPrograms; public class A { protected void m1(){ System.out.println("m1-A"); } } public class B extends A{ public final void m1(){ System.out.println("m1-B"); } public static void main(String[] args) { B b=new B(); b.m1(); A a=new B(); a.m1(); } }
Output: Yes, the code will be successfully compiled.
Key points:
You cannot declare the parent class method as final but the child class method can be declared as final. It is used to restrict further overriding.
a. When b.m1() will be executed, it will call m1() of class B because the reference variable 'b' is pointing to the objects of class B. So, the output will be "m1-B".
b. Since 'a' is also pointing to the objects of class B. Therefore, the output will be "m1-B".
You can get more detail reason in this tutorial- Method overriding in Java.

4. What will be the output of the following program?
    package overridingInterviewPrograms; public class A { synchronized void m1(){ System.out.println("m1-A"); } void m2(char c){ System.out.println("m2-A"); } } public class B extends A{ @Override public final void m1(){ System.out.println("m1-B"); } @Override synchronized void m2(char c){ System.out.println("m2-B"); } public static void main(String[] args) { A a=new B(); a.m1(); a.m2('a'); } }
Output: m1-B, m2-B.
Key points:
When an overridden method is synchronized, the overriding method can be non-synchronized and vice-versa.

5. What will be the output of the following program? 
    package overridingInterviewPrograms; public class X { strictfp void method(int a){ System.out.println("One"); } strictfp void method(double b){ System.out.println("Two"); } } public class Y extends X { @Override void method(double b){ System.out.println("Three"); } } public class Test { public static void main(String[] args) { new Y().method(20); } }
Output: One.
Key points: 
If the parent class method is strictfp, the child class method can be non-strictfp and vice-versa.

6. What will be the output of the following program? 
    package overridingInterviewPrograms; public class X { void draw(int a, float b) throws Throwable{ System.out.println("Circle"); } } public class Y extends X { @Override void draw(int a, float b){ System.out.println("Rectangle"); } } public class Z extends Y { @Override void draw(int a, float b) throws ArithmeticException{ System.out.println("Square"); } } public class Test { public static void main(String[] args) throws Throwable { X x=new Y(); x.draw(20, 30.5f); Y y=(Y)x; y.draw(10,2.9f); Z z=(Z)y; z.draw(20, 30f); } }
Output: Rectangle, Rectangle, and ClassCastException.
Key points: 
1. x.draw(20,30.5f); will call draw method of class Y because the reference variable is pointing to the objects of class Y. Therefore, the output is "Rectangle".
2. When the statement Y y=(Y)x; will be executed, JVM will perform downcasting. When the reference variable of child class refers to the object of a parent class, it is known as downcasting. Here, the reference variable y is pointing to the reference x of the parent class X but x is pointing to the objects of Class Y. So, y is pointing to the objects of class Y. Therefore, y.draw(10, 2.9f); will call draw method of class Y and the output will be "Rectangle".
3. When the statement Z z=(Z)y; will be executed, JVM will throw ClassCastException because Y cannot be cast to Z. 
7. What will be the output of the following program? 
    package overridingInterviewPrograms; public class Animal { void m1(Animal a){ System.out.println("Both dogs and cats are pets."); } } public class Dog extends Animal{ @Override protected void m1(Animal a){ System.out.println("The only dog is a pet animal."); } } public class Cat extends Dog { @Override public void m1(Animal a){ System.out.println("Cat is also a pet animal."); } } public class AnimalClass { public static void main(String[] args) { Animal a=new Cat(); a.m1(null); Dog d=new Dog(); d.m1(a); Cat c=(Cat)new Animal(); c.m1(null); } }
Output: Cat is also a pet animal.
               The only dog is a pet animal.
               Exception in thread "main" java.lang.ClassCastException.

8. Can you find out the error in the below code?
    public class P { static void m1(){ System.out.println("Class P"); } } public class Q extends P{ @Override static void m1(){ System.out.println("Class Q"); } }
Key points: 
The overriding concept is not applicable to a static method.
9. What will be the output of the following program? 
    public class P { void m1(Number n){ System.out.println("m1-P"); } } public class Q extends P { void m1(double d){ System.out.println("m2-Q"); } public static void main(String[] args) { Q q=new Q(); q.m1(1); q.m1(null); } }
Output: m2-Q
              m1-P
10. In the below example program, class Q extends class P. Which method of class Q is not properly overridden in class B? 
    public class P { public Object m1(){ return null; } void m2(Number n){ System.out.println("m1-P"); } } public class Q extends P { public StringBuffer m1(){ return null; } void m2(double d){ System.out.println("m2-Q"); } }
m2() is not properly overridden in class Q because method signature must be the same while overriding.

11. Can you identify the error in the below code snippet? 
    public class P { public Object m1(char c){ return null;
    } } public class Q extends P { public String m1(char c) throws IOException{ return null; } }
Key point: 
If an overridden method does not throw an exception then the overriding method cannot throw any checked or compile-time exception. IOException is a compile-time exception.
For more details, go to this link: Rules of Exceptional handling with method overriding.
12. What will be the output of the following program? 
    public class XY { protected Number m1(int a){ System.out.println("One"); return null; } Object m2(){ System.out.println("Two"); return null; } } public class YZ extends XY{ protected String m2(){ System.out.println("Three"); return null; } } public class XYZ { public static void main(String[] args) { XY xy=new YZ(); xy.m1(20); xy.m2(); } }
Output: One
              Three.
For explanation, go to this link: Covariant return type in Java.

13. In the below snippet code, Is m1() correctly overridden in the subclasses of class X? 
    public class One { void m1(){ } } public class Two extends One { @Override protected void m1(){ System.out.println("m1-Two"); } } public class Three extends Two { @Override public void m1(){ System.out.println("m1-Three"); } }
A. Yes, m1() method is correctly overridden in the subclasses of class X.

14. What will be the output of the following program? 
    package overridingInterviewPrograms; public class One { void m1() throws Throwable{ System.out.println("m1-One"); } } public class Two extends One { @Override protected void m1() throws Exception{ System.out.println("m1-Two"); } } import java.io.IOException; public class Three extends Two { @Override public final void m1() throws IOException{ System.out.println("m1-Three"); } } public class MyTest { public static void main(String[] args) throws Throwable { One o=new Two(); o.m1(); Two t=new Three(); t.m1(); Three th=new Three(); th.m1(); } }
Output: m1-Two
              m1-Three
              m1-Three
15. Can you identify how many errors are in the below snippet code? 
    public class Rose { protected void color(char r){ System.out.println("Red"); } } public class Marigold extends Rose{ @Override void color(char m) throws NullPointerException{ System.out.println("Orange"); } } public class Sunflower extends Marigold { @Override protected final void color(char s) throws IOException{ System.out.println("Yellow"); } }
A. Two.
1. You cannot reduce the visibility of the overriding method in subclass Marigold.
2. You cannot declare the checked exception while overriding in the subclass Sunflower. Marigold is the superclass of Sunflower. 

Final words 
Hope that you will have enjoyed solving all the interview programs based on method overriding. All programs are very important for interview purpose.
Thanks for reading!