0

I tried so hard but I couldn't be able to understand something. I don't get why it's possible to invoke a static method in this way. But it seems to be possible. Moreover I don't get the remaining prints. Can you help me? Thanks a lot.

interface I {
    public void m(A a);

    class IImpl {
        public static void m(B b) {
            System.out.println("m_IB");
        }
    }
}

class A extends I.IImpl implements I {
    public void m(A a) {
        System.out.println("m_AA");
    }
}

class B extends A {
    public void m(A a) {
        super.m(a);
        System.out.println("m_BA");
    }

    public static void m(B b) {
        System.out.println("m_BB");
    }
}

public class Ex3 {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        a.m(b); //prints m_IB but how is it possible this invocation?
        System.out.println();
        I i = new A(); 
        i.m(b); //why does it print m_AA ? I thought It should print m_IBB
        System.out.println();
        I j = new B(); 
        j.m(b); //prints m_AA and m_BA but why isn't called the static method this time?
        System.out.println();
    }
}
  • 3
    It's pretty much just a stupid design decision of the language. If they could go back in time and change it, they probably would. There is no real benefit to changing it now. It would just break backwards compatibility. – Michael Jan 29 '19 at 15:19
  • @Michael we just need to wait for Java 2.0, but I guess I will turn 100 before they decide to throw away backwards compatibility – Lino Jan 29 '19 at 15:24
  • 1
    @Lino Did someone say [Java 2.0](https://kotlinlang.org/)? – Michael Jan 29 '19 at 15:31
  • @Lino They broke backwards compatibility few times, but if I remember correctly it was related to API rather than language syntax (also for that purpose it has `@Deprecated` annotation to signal that some part of API may be removed). Example of *change* (not removal) could be [Why in Java 8 split sometimes removes empty strings at start of result array?](https://stackoverflow.com/q/22718744) – Pshemo Jan 29 '19 at 15:32
  • 1
    @Pshemo The introduction of `var` was a backwards compatibility-breaking change, but only if you were stupid enough to call one of your classes `var`. – Michael Jan 29 '19 at 16:08

0 Answers0