0

In this below code:

interface I1 {
    void m1();
}

interface I2 {
    void m2();
}

abstract class A implements I1, I2 {
    public void m1() {
        System.out.println("Inside A: m1()");
    }
}

class B extends A {
    public void m1() {
        System.out.println("Inside B: m1()");
    }
    public void m2() {
        System.out.println("Inside B: m2()");
    }
}

public class Test {
    
    public static void main(String[] args) {
        
        // invoke A's m1()
        A a = new B();
        a.m1();
        
        
    }
}

How can I just invoke m1() in A without making any changes to class B i.e. we can't add super.m1() in the m1() in B? With my code I just get the m1() in B because of dynamic method dispatch or runtime polymorphism.

An interviewer asked me this question. And I told him that it wasn't possible to do so but he replied that there is a way but didn't told me how.

Shri
  • 109
  • 9
  • you can't. do you understand the purpose of an abstract class? It's sole purpose is to be subclassed. You can't directly instantiate it, so you have to go through subclasses. If you override the method, you override it. If you don't want it overridden, don't override it – Stultuske Nov 23 '22 at 08:43
  • The only way I could think of atm would be to create an anonymous subclass of `A`, e.g. `A a = new A(){ public void m2() {} };`. But not sure if that's what he meant ... if he really knows a way. Some more info on why it wouldn't work with `A a = new B()`: https://stackoverflow.com/questions/6386343/how-to-call-a-super-method-ie-tostring-from-outside-a-derived-class – Thomas Nov 23 '22 at 08:49
  • 1
    The only way I can think of is by using reflection - eg https://stackoverflow.com/questions/5411434/how-to-call-a-superclass-method-using-java-reflection . I do not have a high opinion of whoever thought this would be a good interview question ! – racraman Nov 23 '22 at 09:09
  • @Thomas, A a = new B() would result in the call of m1() in B because the method to be called would depend on the object that the reference variable a is referring to. And in this case, a is referring to an object of type B. Also at the end of the interview when I asked the interviewer about the solution to this question he told me that some concepts related to Type Promotion and Type Conversion would be applied here and asked me to try them. – Shri Nov 23 '22 at 10:19
  • And @Thomas using anonymous inner class I am able to access the m1() in A. This I couldn't think of at that time. – Shri Nov 23 '22 at 10:22
  • Yes, I understand why `A a = new B()` doesn't work - the link I added had some more info on the _why_. :) - I'm not sure what concepts related to type promotion or conversion would be applied here as we're not dealing with primitive types but with classes and polymorphism. There's one more way I could think of but not sure if that would be the intended way: if you can change `A` then just add a parameter to `m1()` and call the method with that parameter. But that would add an overload rather than dealing with an override - if that would be the answer I'd consider it an unfair trick question. – Thomas Nov 23 '22 at 10:29
  • @racraman as far as I know reflection wouldn't work because if you use `A.class.getMethod("m1").invoke(new B())` dynamic dispatch would still happen. – Thomas Nov 23 '22 at 10:31
  • @Thomas Just using invoke, yes - but did you follow up the answers in the question I linked to, that used MethodHandles and https://docs.oracle.com/javase/8/docs/api/java/lang/invoke/MethodHandles.Lookup.html#findSpecial-java.lang.Class-java.lang.String-java.lang.invoke.MethodType-java.lang.Class- – racraman Nov 23 '22 at 10:44
  • @racraman oh I see. That would seem to work but it's definitely something I'd not want people to do - only as a last resort and only by those that really know what they're doing. I'd hope the average Java developer doesn't even know how to use reflection :D – Thomas Nov 23 '22 at 10:48
  • @Thomas Exactly - I think you share my opinion of that as an interview question ! – racraman Nov 23 '22 at 13:07

0 Answers0