1

lets research following code:

public class App {

    public static class A {

        public void doSmth3(long a) {
            System.out.println("This is doSmth3() in A...");
        }
    }

    public static class B extends A {

        public void doSmth3(int a) {
            System.out.println("This is doSmth3() in B...");
        }
    }

    public static void test(A a) {
        a.doSmth3(1);
    }

    public static void main(String[] args) {
       test(new B());
        new B().doSmth3(3);
    }

}

ouput:

This is doSmth3() in A...
This is doSmth3() in B...

from my side 2 lines in main should provide same result but result is different.

My opininion This is doSmth3() in A... should output twise because it is overloading.

Please explain output

2
  • 1
    3 is an int, this means the method with the int parameter will be called. If you change it to 3L it will be a long, then the method with the long parameter will be called (always the 'most fitting' method will be called if you overload it. Commented Oct 23, 2015 at 16:30
  • IMO the reason for the confusion was your naming conventions... this is really simple code but it is hard to read because you are reusing numbers and letters.... Commented Oct 23, 2015 at 16:45

3 Answers 3

6

Simple: when the Java compiler sees the call to a.doSmth3(1) inside test(A), it can only possibly compile it to a call to A#doSmth3(long), which is the only method available. Note that B#doSmth3(int) is an overload of A#doSmth3(long), not an override.

Sign up to request clarification or add additional context in comments.

Comments

1

Class A contains the following method:

public void doSmth3(long a)

Class B contains the following methods:

public void doSmth3(long a) // Inherited from class A
public void doSmth3(int a)  // This is an overload of doSmth3

When you call a.doSmth3(1);, a is referenced with type A. Hence, it has to call method with the long argument. The other method using int is not available for an object referenced as A.

When you call new B().doSmth3(3);, the object is referenced as a type B. It contains both methods. Since you pass a int value, it uses the method with the int argument.

Comments

0

When you overload, method selection is based on the type or parameters, and the decision to implicitly cast types is made at compile time. Therefore, test(new B()); runs doSmth3(long) because that is the only method available to A and

public static void test(A a) {
    a.doSmth3(1);
}

has to choose a method available to A.

But when you call new B().doSmth3(3); you have all methods of A and B to choose from and doSmth3(int) in B is a better fit for an int literal than doSmth3(long). So that why you print from B, because your input is an int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.