Java polymorphism when passing variables. [duplicate] - java

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.

Related

Can void method be defined using dot notation? [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 5 years ago.
I've just started learning Java and I am stuck at this MCQ:
Assume class Temp is defined as belowand the statment Temp a= new temp() is
successfully executed Which of the statement is illegal in Java?
class Temp {
public static int i;
public void method1() { }
public static void method2() { }
}
A. System.out.println(i);
B. Temp.method1();
C. a.method1();
D. Temp.method2();
The answer is B, but I can't understand why. Is it because a void method cannot be defined using the dot notation unless it's static?
Because method1 is non-static method. You can use methods with class names only if they are static. Look here for more details.

interfaces having same function prototype in java? [duplicate]

This question already has answers here:
Java - Method name collision in interface implementation
(7 answers)
Closed 7 years ago.
I have implemented two interfaces with same method names and signatures in a single class then how the compiler will identify the which method is for which interface?
E.X:
public interface Hourly{
int calculate_salary();
}
public interface Fixed{
int calculate_salary();
}
public class Employee implements Hourly, Fixed{
public static void main(String... args) throws Exception{
}
#Override
int calculate_salary(){ // from which interface Hourly or Fixed???
return 0;
}
}
this problem has solution in C# but it did not work that way in java please help
thanks
There is no choice to be made.
There can only be one actual implementation of this method, and it will be called. The fact that the class implements two interfaces that both force it to have such a method doesn't meant that there must be (or can be) two such methods. The existence of the one method satisfies the conditions imposed by both interfaces.

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).

Java function overloading query [duplicate]

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".

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