tricky method Overloading in java [duplicate] - java

This question already has an answer here:
Method overloading/overriding in Java subclass object with superclass reference variable
(1 answer)
Closed 6 years ago.
class ABC {
void doMe(String s) {
System.out.println("String");
}
}
class XYZ extends ABC {
void doMe(Object o) {
System.out.println("Object");
}
}
public class StopStart {
public static void main(String[] args){
ABC o = new XYZ();
o.doMe(null);
}
}
What will happen and why?
string class extends Object. So which doMe() will execute.
Is it a compilation error?

Overloading is resolved in compile time, according to the compile time type of the variable o. Therefore doesn't matter which methods are defined in the XYZ sub-class. Only the methods of ABC are relevant.
Therefore only void doMe(String s) is considered by the compiler, and that's the chosen method.

Related

I am trying this code in Java but the result output is not clear to me. can someone please explain the reason for below output [duplicate]

This question already has answers here:
Why child class method is not called in this example as it is called in dynamic polymorphism in Java?
(4 answers)
Closed 2 years ago.
Below is the code I'm trying to run. If I remove the parameters from func() methods than the obj.func() call B class method but if I introduce the parameters it calls A class method.
public class A {
void func(A obj) {
System.out.println("in A");
}
}
public class B extends A{
void func(B obj) {
System.out.println("in B");
}
public static void main(String[] args) {
A obj = new B();
obj.func(null);// calls A class method, but why???
}}
I suppose that you are trying to override method func of class A in class B. Anyway, your code is overloading the method, because the type of the parameters of A.func and of B.func are different. The former receives an object obj of type A, while the second receives an object obj of type B.
public class A {
void func(A obj) {
System.out.println("in A");
}
}
public class B extends A{
void func(B obj) { // your solution, which creates a DIFFERENT method using overloading
System.out.println("OVERLOAD in B");
}
void func(A obj) { // proper override of *A.func(A obj)*
System.out.println("OVERRIDE in B");
}
} // <- pay attention, you forgot this bracket
public static void main(String[] args) {
A obj = new B();
obj.func(null);// now it calls the right method
}}
If you remove the parameter of func, the two functions are identical, hence you have a proper override. That is why removing the argument the code worked properly.
I hope I was able to help you, anyway you can find more information here:
Method overloading, that is what your previous code was doing: https://www.javatpoint.com/method-overloading-in-java
Method overriding, that was your target: https://www.javatpoint.com/method-overriding-in-java
Runtime polymorphism, that is what happens when you write A obj = new B(); https://www.javatpoint.com/runtime-polymorphism-in-java
Have a good day!

Static / Compile-time Polymorphism [duplicate]

This question already has answers here:
What is the difference between dynamic and static polymorphism in Java?
(14 answers)
Closed 3 years ago.
Why method overloading called as static or compile-time polymorphism
sample in Java.
class StaticPolymorphismSample {
void polymorphicMethod(int a) {
}
void polymorphicMethod(int a, int b) {
}
void polymorphicMethod(String a) {
}
void nonPolymorphicMethod(int a) {
}
void nonPolymorphicMethod1(int a) {
}
}
so my question is.
Why we say that method overloading ( in this case polymorphicMethod methods ) are static polymorphism , but another methods( nonPolymorphicMethod(int a) nonPolymorphicMethod1(int a) ) are not polymorphism.
technically I cannot see different between method with same name and different parameters and method with different,
all answers in here and topics in google is not applicable for my question.
For nonPolymorphicMethod1(int a) the reason this wouldn't be considered polymorphic is because it has a different name from the other nonPolymorphicMethods.
For nonPolymorphicMethod( int a, int b ) and nonPolymorphicMethod( int a ) these aren't considered polymorphic as they don't take the same parameters. Edit This is Wrong See Next Line
The other methods you've shown are polymorphic due to their sharing of a name, but differing parameter types or number of parameters.
A better example of polymorphism in methods would be :
public abstract class ClassA
{
public Object getObject()
{
return new Object();
}
}
public class ClassB extends ClassA
{
#Override
public ClassB getObject()
{
return new ClassB();
}
}
public class ClassC extends ClassA
{
#Override
public ClassC getObject()
{
ClassC example = new ClassC();
example.doStuff();
return example;
}
private void doStuff()
{
// Do Something To Change The Object
}
}

Are these two method calls the same? [duplicate]

This question already has an answer here:
What are the advantages of an anonymous object?
(1 answer)
Closed 6 years ago.
Code blocks number 1.
public class SomeClass {
public static void main(String [] args) {
SomeClass foo = new SomeClass();
foo.SomeMethod();
}
public void SomeMethod() {
}
}
Code blocks number 2.
public class SomeClass {
public static void main(String [] args) {
new SomeClass().SomeMethod();
}
public void SomeMethod() {
}
}
Are this two code blocks(number 1 and 2) the same? I'm confused over the different syntax during calling the method. I appreciate if someone could explain it for me.
Yes, they are functionally the same. With code block 2, however, you have no way of accessing the SomeClass object you created in the future lifespan of the program.
Yes they are same. In first case the object reference is just stored in a reference variable for future usage .
In second case the reference is not stored.

Unexpected overloaded method compiler selection with null parameter [duplicate]

This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 6 years ago.
I am very surprised why output is very much different from what I am expecting , I have two overloaded methods, one having one String and the other an Object as parameter, while calling this method with null parameter, output is just printing
"String"
and not calling method having object as parameter.
Why does Java selects the method having String as parameter, how java determines which overloaded method to call ?
class TestingClass {
public void test(String s) {
System.out.println("String");
}
public void test(Object o) {
System.out.println("Object");
}
public static void main(String[] args) {
TestingClass q = new TestingClass();
q.test(null);
}
}
There is no overriding in this code, only method overloading. When the compiler has to choose which of the two test methods to execute (since null can be passed to both), it chooses the one with the more specific argument type - test(String s) - String is more specific than Object since it is a sub-class of Object.
The other method can be called using :
q.test((Object) null);
Without adding a possible new answer, let me suggest you to extend your code like following,
public class Overload {
public static void main(String[] args) {
new Overload().test(null);
}
public void test(String s) {
System.out.println("String");
}
public void test(Object o) {
System.out.println("Object");
}
public void test(Integer s) {
System.out.println("Integer");
}
}
Comment out any one of Object or Integer version any see it for yourself. The answer being already provided by others.

Why can null be passed to an overloaded Java method? [duplicate]

This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 7 years ago.
I found this Java code from this site. I don't understand how it compiles without ambiguous error.
package swain.test;
public class Test {
public static void JavaTest(Object obj) {
System.out.println("Object");
}
public static void JavaTest(String arg) {
System.out.println("String");
}
public static void main(String[] args) {
JavaTest(null);
}
}
Output:
String
null can be passed to both JavaTest(String arg) and JavaTest(Object obj), so the compiler chooses the method with the more specific argument types. Since String is more specific than Object (String being a sub-class of Object), JavaTest(String arg) is chosen.
If instead of JavaTest(Object obj) you had JavaTest(Integer obj), the compilation would have failed, since Integer is not more specific than String and String is not more specific thanInteger`, so the compiler wouldn't be able to make a choice.

Categories