Overriding Question asked in interviews


Intent of my Blog

Overriding is the concept which is very much asked in the interview.In almost every interview i have given or taken overriding questions were present.So, i decided to document all the possible overriding questions.Try out these questions and have fun.

What is Overriding?

According to wikipedia

“Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.”

Overriding Questions

Question 1

public class OverridingQuestion1 {

 public static void main(String[] args) {
 A a = new A();
 a.execute();
 B b = new B();
 b.execute();
 a = new B();
 a.execute();
 b = (B) new A();
 b.execute();
 }

}

class A {
 public void execute() {
 System.out.println("A");
 }
}

class B extends A {
 @Override
 public void execute() {
 System.out.println("B");
 }
}

Question 2


class A1 {

 private void prepare(){
 System.out.println("Preparing A");
 }
}

class B1 extends A1 {

 public void prepare(){
 System.out.println("Preparing B");
 }
 public static void main(String[] args) {
 A1 a1 = new B1();
 a1.prepare();
 }
}

Question 3

public class OverridingQuestion3 {

 public static void main(String[] args) {
 A2 a2 = new A2();
 System.out.println(a2.i);
 B2 b2 = new B2();
 System.out.println(b2.i);
 a2 = new B2();
 System.out.println(a2.i);
 }

}
class A2{
 int i = 10;

}
class B2 extends A2{
 int i = 20;

}

Question 4


public class OverridingQuestion4 {

 public static void main(String[] args) {
 A3 a3 = new A3();
 a3.execute();
 B3 b3 = new B3();
 b3.execute();
 a3 = new B3();
 a3.execute();
 b3 = (B3)new A3();
 b3.execute();
 }

}

class A3{
 public static void execute(){
 System.out.println("A3");
 }

}
class B3 extends A3{
 public static void execute(){
 System.out.println("B3");
 }
}

Question 5


public class OverridingQuestion5 {

 public static void main(String[] args) {
 A4 a4 = new B4();
 a4.execute();
 }

}

class A4{
 public void execute() throws Exception{
 System.out.println("A");
 }
}
class B4 extends A4{
 @Override
 public void execute(){
 System.out.println("B");
 }
}

As if now i can think of the above 5 questions. If you have any other overriding question, please put that in comment.
I am not posting solution to these questions. So, try these, and post the solution in comments.

7 thoughts on “Overriding Question asked in interviews

  1. clandestino_bgd

    1. ClassCastException (line 10)
    2. compile error (line 14)
    3. 10, 20, 10
    4. A3, B3, A3 , ClassCastException (line 10)
    5. compile error (line 5)

    Reply
  2. Nyarla

    1: A,B,B,ClassCastException
    2: Compile time error
    3: 10,20,10
    4: A3,B3,A3,ClassCastException
    5: Compile time error

    Explanations:
    1:
    Line 05: The Object in “a” is of type “A” and it will print “A”
    Line 07: The Object in “b” is of type “B” and it will print “B”
    Line 09: The Object in “a” is now of type “B” and so the overridden method will be called and print “B”
    Line 10: An Object of type “A” can’t be casted to “B”.

    2:
    Line 10: Class “B” defines a “prepare”-Method which doesn’t override the “A.prepare”-Method, since that method is private!
    Line 15: The call to the private “prepare”-method of “A” can’t be executed in another class (“B”).

    3:
    Line 09: Since variables can’t be overridden, the compiler wires the access to the A.i-Variable, even if the Object is a subclass with it’s own variable
    (Hint: very, VERY, *VERRRYY* bad style! Use Getters and Setters!)

    4:
    Lines 05,07,09: Static variables and methods aren’t bound to objects, they can even be called when the variable is “null”.
    Line 10: An Object of type “A3” can’t be casted to “B3”.

    5:
    Line 17: Overriding works just normal, the overriding method don’t need to define all exceptions or can define more specific exceptions without breaking the contract
    Line 05: The compiler expects any object of “A4” at variable “a4”, so he must expect an Exception to be thrown from the method.

    Reply
  3. Angelo vd Sijpt

    Q1
    A
    B
    B
    ClassCastException

    Q2
    Compile error: a1’s prepare, even though you create a B1, is not visible, since it is private. I guess you want to show that private methods cannot be overriden, right?

    Q3
    10
    20
    10
    Nice one, shows that members are not governed by overriding rules. I actually fired up an Eclipse to check this for sure…

    Q4
    A3
    B3
    A3
    ClassCastException

    Q5
    You will get a compile error: A’s execute can throw an exception, and since the type of a4 is a that of A4, you will have to handle that. If you do, the output will be B.

    Nice questions, that well show the concepts you just need to know, or at least be aware of!

    Another nice feature to show is that you are allowed to increase visibility of methods, but not decrease it:
    class A {
    public void doStuff { System.out.println(“A”); }
    }
    class B extends A {
    protected void doStuff { System.out.println(“B”); }
    }

    You could also add something about final methods, and if you want to induce headaches, you can show that method resolving for inheritance is influenced by method visibility (i.e., you can only really override a method of it is visible to you, which might be hampered by methods which have package visiblity). Recently, there has been a post about that, but I can’t find it right now.

    Reply
  4. Pingback: Java Puzzlers on local variable initialization « My Thoughts on software development

  5. Who Cares

    What do you gain from asking these questions???? What does that prove? Does that prove the candidate is a great programmer or can actually write software? I think programming logic and concepts like OO design, patterns should be the focus and not some silly stupid language questions.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s