I couldn�t find the different between example 1 and 2. But the example 1 complies and run�s fine but the example 2 gives compilation error. I would like to know what is happening???
Error message of Example 2:- C:\java>javac Test.java Test.java:14: reference to getVal is ambiguous, both method getVal(int,int) in Base and method getVal(long,long) in Derived match System.out.println(d.getVal(10, 7));
When you have a literal number in your source code, it's an int. You can see below how to have a literal long.
Here's the kicker: while polymorphism (overriding methods) is done during runtime, overloading is done at compile time. Therefore, whatever your variable is typed as will determine which method is run. Since your literal is an int, it calls the int-overloaded method. You can cast it as a long if you'd like to get the long-overloaded method.
This is best illustrated when you have a null value, but want to invoke a specifically-overloaded method. (Which isn't really the best design-wise, but oh well.)
You should get a compile-time error saying the call to invoke is ambiguous. There's no type associated with a null value, so it doesn't know which method to call (a static, compile-time problem).
Fix this by casting the null value to either String or Date.