Java function overloading query [duplicate] - java

This question already has answers here:
Overloaded method selection based on the parameter's real type
(7 answers)
Closed 8 years ago.
A extends B
public class Test {
public static void print(A obj) {
System.out.println("print A");
}
public static void print(B obj) {
System.out.println("print B");
}
public static void main(String [] args ) {
A x = new B();
print(x);
}
}
Why it'll print "print A"?
Why function overloading doesn't look up the real type of x in runtime?

Because overloading is not overriding. The compiler binds methods based on the declared types of arguments.

Why function overloading doesn't look up the real type of x in runtime?
Because function overloading doesn't look up the real type of x at runtime, by JLS #8.4.9: "the number of actual arguments (and any explicit type arguments) and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked".

Related

Some problems with passing arguments to overloaded functions [duplicate]

This question already has answers here:
Passing null to the Overloaded methods [duplicate]
(3 answers)
How to do method overloading for null argument?
(7 answers)
Closed 4 years ago.
package java_coding;
public class String_Null {
public static void main(String[] args) {
// TODO Auto-generated method stub
String_Null string_Null = new String_Null();
string_Null.method(null);
}
public void method(String string) {
System.out.println("String");
}
public void method( Object object) {
System.out.println("Object");
}
}
The result is String. Explain why?
Because the most specific method is chosen, the most specific is String (compared to Object). This is in the JLS btw... I'll dig this up shortly.
According to the JLS
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run- time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.

Java polymorphism when passing variables. [duplicate]

This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Closed 4 years ago.
class X{
public void print(X x){System.out.println("xx");}
public void print(Y y){System.out.println("xy");}
}
class Y extends X{
public void print(X x){System.out.println("yx");}
public void print(Y y){System.out.println("yy");}
public static void main(String[] args){
X x = new Y();
x.print(x);
System.out.println(x.getClass());
}
}
The output i get is "yx" end I dont understand why, x.getClass() returns "class Y" so shouldn't its call the method where the parameter is Y?
Overloads (i.e. choice which of multiple methods of the same class/interface with the same name to use) are resolved based on static types. This is in opposition to overriding (choice between X#print(X) and Y#print(X) which is resolved at runtime.
Dynamic binding doesn't work for method parameters, hence parameter resolution of method is done in compile time, and the compiler resolves with the static type X.

executing sequence of static methods in java [duplicate]

This question already has answers here:
How do overloaded methods work?
(2 answers)
Closed 8 years ago.
I have a dummy program in java given below-
public class DummyTest {
public static void main(String[] args) {
hungry(null);
}
public static void hungry(Object o){
System.out.println("object");
}
public static void hungry(String s){
System.out.println("string");
}
}
this program returns prints string. please tell me the concept that why it prints string and not object.
This is how method overloading works. When the parameter of one candidate is more specific than the parameter of the other (as String is more specific than Object), the method with the more specific parameter is chosen.
Note that if you add a 3rd hungry method with a parameter of reference type unrelated to String (for example Integer), the code won't pass compilation since the compiler won't have a preference between hungry(String) and hungry(Integer).

Passing null reference in a method parameter in java? [duplicate]

This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
When i run this code the output is "String"; if I hide the method that accepts a String parameter and run the code again then the output is "Object", so can anyone please explain me how this code works?
public class Example {
static void method(Object obj) {
System.out.println("Object");
}
static void method(String str) {
System.out.println("String");
}
public static void main(String args[]) {
method(null);
}
}
Compiler will choose most specific method, in this case, String is a sub class of Object, so the method with String as argument will be invoked.
From JLS 15.12.2.5
If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than
another if any invocation handled by the first method could be passed
on to the other one without a compile-time type error.

What is the output for the following Java program where null is passed as argument for the overloaded methods? [duplicate]

This question already has answers here:
Java method dispatch with null argument
(4 answers)
Closed 9 years ago.
public class Overloading {
public static void main(String[] args) {
Overloading o = new Overloading();
o.display(null);
}
void display(String s) {
System.out.println("String method called");
}
void display(Object obj) {
System.out.println("Object method called");
}
}
It is giving the output as "String method called". I need the explanation why?
Taken from the Java Spec:
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
First of all: both methods are accessible (obviously) and applicable. They are both applicable, because null is of type nulltype, which is by definition a subtype of all types. String is more specific than Object, because String extends Object. If you would add the following, you will have a problem, because both Integer and String are equally "specific":
void display(Integer s) {
System.out.println("Integer method called");
}
Java will always find the most specific version of method that is available.
String extends Object, and therefore it is more specific.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

Categories

Resources