Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding - Parameter difference

public class A{
    public static int x = 1;
    public int m(A a, B b){
        return a.m(b, b) + x;
    }
}

public class B extends A {
    public int m(A a1, B a2){
        if(a1 == a2)
            return x;
        return super.m(a1,a2);
    }
}

This is a question from a past exam of a subject I am taking.

public class Main{
    public static void main(String[] args){
        B b1 = new B(){int m(A a, A b){ return 10; }};
        System.out.println(b1.m(b1, b1));
    }
}

The question is, what does the following output. I was correct in the answer of 1. But I failed to fully understand why.

Since the inner class of the B object is (A,A). Am i correct in thinking it can not override the super method m since it is (A,B)? Would it be able to override, if the parameters of the two methods were swapped?

Since it can neither override or overload, it does nothing and simply uses the method m in B class?

Does the inner class of the object, only work for itself? Is it like an anon class?

Apologises about all the questions.

Thanks in advance.

Edit:

From my understanding so far, its because the static type is set to B, so the type B is unable to see the anon class. If this was set to public, it would be able to be seen, but it still wouldn't be used.

This confused me on another question.

public class A{
    public static int x = 1;
    public int m(A a, B b){
        return a.m(b, b) + x;
    }
}

public class Main{
 public static void main(String[] args){
  A a1=new A();
  A a2=new A(){ 
   int m(A a,B b){
    return 10;
   }};
  B b1=new B();
  System.out.println(a1.m(a2,b1));
 }
}

When the following is called, the output is 11. When a1.m is called, it passes a2 and b1.

In the A class, when a.m(b,b) is called. It calls the dynamic type. Is this because it changes to the dynamic type once parsed? So now it is able to use the anon class?

like image 403
user2469515 Avatar asked Apr 19 '26 15:04

user2469515


2 Answers

Let us walk through the JVM:

B b1 = new B(){ int m(A a,A b) { return 10; } };

This creates an instance of B with an anonymous overload of method m() having two arguments of type A. But this is a different m() method than what class B (or A) defines, since their signature (A, A vs A, B) differ.

Then:

System.out.println(b1.m(b1, b1));

This calls b1's m() method with two arguments of type B.

Now the JVM looks up what m() is:

  • there is no method m() having two arguments of type B;
  • in class B, it finds a match: a method by name m() whose first argument is a A (since B inherits A) and a second argument which is of type B;
  • the anonymous overloading by b1 is completely ignored here! (see https://gist.github.com/fge/5762353)
  • end of method lookup.

The m() method of class B is therefore invoked. And in this method, we have:

if (a1 == a2) // TRUE!
   return x;

Since the condition is true (the method has been called with the two arguments being the exact same object references), the value of x must be returned; and x is defined by class A, which class B extends:

 public static int x = 1;

Since this is a static member of class A (which class B does not hide) it is equally accessible by instance methods, from any instance of A or B; therefore, the return code, and output of this program, is 1.

like image 102
fge Avatar answered Apr 21 '26 07:04

fge


Am i correct in thinking it can not override the super method m since it is (A,B)?

Yes, you are correct: this creates an overload, not an override of the method in the base class.

Would it be able to override, if the parameters of the two methods were swapped?

No: to override, the method signatures must match exactly.

Since it can neither override or overload, it does nothing and simply uses the method m in B class?

Not exactly: the (A, B) overloads happens to be more specific than the (A, A) overload.

Does the inner class of the object, only work for itself? Is it like an anon class?

You would be able to invoke the method in the inner class, had you passed it a pair of As.

like image 25
Sergey Kalinichenko Avatar answered Apr 21 '26 05:04

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!