1

This question was asked to me in an interview. I have an overridden method in my sub-class. Using an instance of the subclass, I want to call the method of the super class. Bear in mind that the method is overridden.

The output of the following code is NO NO PRINT HI.
What if I want to print Print Hello using the object of overriding class? How do I do that?

class OverrideSuper {
    public void printHello() {
         System.out.println("Print Hello");
    }
}

public class Overriding extends OverrideSuper {
    public void printHello() {
         System.out.println("NO NO PRINT HI");
    }

    public static void main(String[] args) {
        //OverrideSuper obj1 = new OverrideSuper();
        Overriding obj2 = new Overriding();
        obj2.printHello();// this calls printHello() of class Overriding.
        //I want to call printHello() of OverrideSuper using obj2. How do I do that???
    }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Mohit
  • 31
  • 5

3 Answers3

4

I think the interviewer expected you to write another method that calls super.printHello():

public class Overriding extends OverrideSuper {
    public void printHello(){
         System.out.println("NO NO PRINT HI");
    }
    public void superHello() { // You can make this private or protected, too
        super.printHello();
    }
    public static void main(String[] args) {
        //OverrideSuper obj1 = new OverrideSuper();
        Overriding obj2 = new Overriding();
        obj2.superHello();// this calls printHello() of class OverrideSuper .
    }
}

Essentially, he wanted you to tell him that once a method is overriden, there is no way to call through to the method of the base class from the outside. The only way around it is to make a calling path into the superclass on the inside of the overriding class, which is what I did above by defining a superHello method.

[The interviewer] said "I am not convinced"

Another possibility could be that the interviewer was looking for you to provide a reflection-based solution, which is possible only in Java 7+. You need to use MethodHandle, call findSpecial with the base class, and then invoke the method that you get back.

Here is a link to an answer that explains the process, and shows an example.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Excatly, this is what I actually did answer. However, the interviewer was constantly irritating me by saying " No No I want to call from obj2." When I said its not possible. He said " I am not convinced"... Any case I know that I was correct. Thanks a lot people :) Have a good day.... :) – Mohit Feb 03 '14 at 20:24
1

i think this will fulfil your requirement!! try this when you call obj2.printHello(); execution will move to Overridings printHello() and in that method i ve used super.printHello(); this means first invoke super class's printHello() and compiler prints Print Hello then execution again come to Overridings printHello() and print NO NO PRINT HI

class OverrideSuper {
    public void printHello() {
        System.out.println("Print Hello");
    }
}
    public class Overriding extends OverrideSuper {
            public void printHello() {
                super.printHello();
                System.out.println("NO NO PRINT HI");
            }

            public static void main(String[] args) {
                // OverrideSuper obj1 = new OverrideSuper();
                Overriding obj2 = new Overriding();
                obj2.printHello();// this calls printHello() of class Overriding.
                // I want to call printHello() of OverrideSuper using obj2. How do I do
                // that???
            }
        }

output

Print Hello
NO NO PRINT HI
Mohsin AR
  • 2,998
  • 2
  • 24
  • 36
-2

You can use the super keyword to access the parent method.

class OverrideSuper{
    public void printHello(){
         System.out.println("Print Hello");
    }
}
public class Overriding extends OverrideSuper {
    public void printHello(){
        super.printHello();
        //System.out.println("NO NO PRINT HI");
    }
    public static void main(String[] args) {
        //OverrideSuper obj1 = new OverrideSuper();
        Overriding obj2 = new Overriding();
        obj2.printHello();// this calls printHello() of class Overriding.
        //I want to call printHello() of OverrideSuper using obj2. How do I do that???
    }
}
user1389749
  • 191
  • 1
  • 3