Java Function Call with Overload
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I want to know why the third output is NOT b.
Here is my code:
public class SimpleTests
public void func(A a)
System.out.println("Hi A");
public void func(B b)
System.out.println("Hi B");
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
SimpleTests i = new SimpleTests();
i.func(a);
i.func(b);
i.func(c);
class A
class B extends A
And here is the output:
Hi A
Hi B
Hi A
Could someone tell me why the 3rd output is Hi A
, NOT Hi B
. as the real c is a instance of B.
java method-overloading
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
7
down vote
favorite
I want to know why the third output is NOT b.
Here is my code:
public class SimpleTests
public void func(A a)
System.out.println("Hi A");
public void func(B b)
System.out.println("Hi B");
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
SimpleTests i = new SimpleTests();
i.func(a);
i.func(b);
i.func(c);
class A
class B extends A
And here is the output:
Hi A
Hi B
Hi A
Could someone tell me why the 3rd output is Hi A
, NOT Hi B
. as the real c is a instance of B.
java method-overloading
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
See stackoverflow.com/questions/383947/…
– Raedwald
58 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I want to know why the third output is NOT b.
Here is my code:
public class SimpleTests
public void func(A a)
System.out.println("Hi A");
public void func(B b)
System.out.println("Hi B");
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
SimpleTests i = new SimpleTests();
i.func(a);
i.func(b);
i.func(c);
class A
class B extends A
And here is the output:
Hi A
Hi B
Hi A
Could someone tell me why the 3rd output is Hi A
, NOT Hi B
. as the real c is a instance of B.
java method-overloading
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I want to know why the third output is NOT b.
Here is my code:
public class SimpleTests
public void func(A a)
System.out.println("Hi A");
public void func(B b)
System.out.println("Hi B");
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
SimpleTests i = new SimpleTests();
i.func(a);
i.func(b);
i.func(c);
class A
class B extends A
And here is the output:
Hi A
Hi B
Hi A
Could someone tell me why the 3rd output is Hi A
, NOT Hi B
. as the real c is a instance of B.
java method-overloading
java method-overloading
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago


khelwood
28.6k63858
28.6k63858
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
cceasy
435
435
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
cceasy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
See stackoverflow.com/questions/383947/…
– Raedwald
58 mins ago
add a comment |Â
See stackoverflow.com/questions/383947/…
– Raedwald
58 mins ago
See stackoverflow.com/questions/383947/…
– Raedwald
58 mins ago
See stackoverflow.com/questions/383947/…
– Raedwald
58 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
7
down vote
Calls to overloaded methods are resolved based on the reference type (A
) of the argument at compile time, not the object type (B
) at runtime. You declared the variable to be of type A
, so it is treated as type A
.
add a comment |Â
up vote
1
down vote
You're confusing overloading with polymorphism.
With polymorphism, when creating an instance of class B which is a subclass of class A, referenced to by an class A object, and overwrites the method of class A, calling the method will perform the method of class B.
With overloading, the called method only knows the type of the declaration of the argument, not the initialization.
public class A
public void print()
System.out.println("A");
public class B extends A
@Override
public void print()
System.out.println("B");
public class Main
public static void main(String args)
A a = new A();
B b = new B();
A otherB = new B();
a.print();
b.print();
otherB.print();
This will output
A
B
B
add a comment |Â
up vote
1
down vote
JLS §8.4.9. Overloading:
When a method is invoked, 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.
The compile-time types in your example are:
A a = new A(); // A
B b = new B(); // B
A c = new B(); // A
add a comment |Â
up vote
0
down vote
Reason being as said by khelwood, further to this if you want to return the Output.
Hi A
Hi B
Hi B
Just pass the object instead of reference, It will then call function B, as there is no resolution at compile time.
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
test i = new test();
i.func(a);
i.func(b);
i.func(new B());
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
Calls to overloaded methods are resolved based on the reference type (A
) of the argument at compile time, not the object type (B
) at runtime. You declared the variable to be of type A
, so it is treated as type A
.
add a comment |Â
up vote
7
down vote
Calls to overloaded methods are resolved based on the reference type (A
) of the argument at compile time, not the object type (B
) at runtime. You declared the variable to be of type A
, so it is treated as type A
.
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Calls to overloaded methods are resolved based on the reference type (A
) of the argument at compile time, not the object type (B
) at runtime. You declared the variable to be of type A
, so it is treated as type A
.
Calls to overloaded methods are resolved based on the reference type (A
) of the argument at compile time, not the object type (B
) at runtime. You declared the variable to be of type A
, so it is treated as type A
.
edited 58 mins ago
answered 1 hour ago


khelwood
28.6k63858
28.6k63858
add a comment |Â
add a comment |Â
up vote
1
down vote
You're confusing overloading with polymorphism.
With polymorphism, when creating an instance of class B which is a subclass of class A, referenced to by an class A object, and overwrites the method of class A, calling the method will perform the method of class B.
With overloading, the called method only knows the type of the declaration of the argument, not the initialization.
public class A
public void print()
System.out.println("A");
public class B extends A
@Override
public void print()
System.out.println("B");
public class Main
public static void main(String args)
A a = new A();
B b = new B();
A otherB = new B();
a.print();
b.print();
otherB.print();
This will output
A
B
B
add a comment |Â
up vote
1
down vote
You're confusing overloading with polymorphism.
With polymorphism, when creating an instance of class B which is a subclass of class A, referenced to by an class A object, and overwrites the method of class A, calling the method will perform the method of class B.
With overloading, the called method only knows the type of the declaration of the argument, not the initialization.
public class A
public void print()
System.out.println("A");
public class B extends A
@Override
public void print()
System.out.println("B");
public class Main
public static void main(String args)
A a = new A();
B b = new B();
A otherB = new B();
a.print();
b.print();
otherB.print();
This will output
A
B
B
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You're confusing overloading with polymorphism.
With polymorphism, when creating an instance of class B which is a subclass of class A, referenced to by an class A object, and overwrites the method of class A, calling the method will perform the method of class B.
With overloading, the called method only knows the type of the declaration of the argument, not the initialization.
public class A
public void print()
System.out.println("A");
public class B extends A
@Override
public void print()
System.out.println("B");
public class Main
public static void main(String args)
A a = new A();
B b = new B();
A otherB = new B();
a.print();
b.print();
otherB.print();
This will output
A
B
B
You're confusing overloading with polymorphism.
With polymorphism, when creating an instance of class B which is a subclass of class A, referenced to by an class A object, and overwrites the method of class A, calling the method will perform the method of class B.
With overloading, the called method only knows the type of the declaration of the argument, not the initialization.
public class A
public void print()
System.out.println("A");
public class B extends A
@Override
public void print()
System.out.println("B");
public class Main
public static void main(String args)
A a = new A();
B b = new B();
A otherB = new B();
a.print();
b.print();
otherB.print();
This will output
A
B
B
answered 1 hour ago


SBylemans
589216
589216
add a comment |Â
add a comment |Â
up vote
1
down vote
JLS §8.4.9. Overloading:
When a method is invoked, 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.
The compile-time types in your example are:
A a = new A(); // A
B b = new B(); // B
A c = new B(); // A
add a comment |Â
up vote
1
down vote
JLS §8.4.9. Overloading:
When a method is invoked, 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.
The compile-time types in your example are:
A a = new A(); // A
B b = new B(); // B
A c = new B(); // A
add a comment |Â
up vote
1
down vote
up vote
1
down vote
JLS §8.4.9. Overloading:
When a method is invoked, 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.
The compile-time types in your example are:
A a = new A(); // A
B b = new B(); // B
A c = new B(); // A
JLS §8.4.9. Overloading:
When a method is invoked, 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.
The compile-time types in your example are:
A a = new A(); // A
B b = new B(); // B
A c = new B(); // A
answered 11 mins ago


Oleksandr
7,34533366
7,34533366
add a comment |Â
add a comment |Â
up vote
0
down vote
Reason being as said by khelwood, further to this if you want to return the Output.
Hi A
Hi B
Hi B
Just pass the object instead of reference, It will then call function B, as there is no resolution at compile time.
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
test i = new test();
i.func(a);
i.func(b);
i.func(new B());
add a comment |Â
up vote
0
down vote
Reason being as said by khelwood, further to this if you want to return the Output.
Hi A
Hi B
Hi B
Just pass the object instead of reference, It will then call function B, as there is no resolution at compile time.
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
test i = new test();
i.func(a);
i.func(b);
i.func(new B());
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Reason being as said by khelwood, further to this if you want to return the Output.
Hi A
Hi B
Hi B
Just pass the object instead of reference, It will then call function B, as there is no resolution at compile time.
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
test i = new test();
i.func(a);
i.func(b);
i.func(new B());
Reason being as said by khelwood, further to this if you want to return the Output.
Hi A
Hi B
Hi B
Just pass the object instead of reference, It will then call function B, as there is no resolution at compile time.
public static void main(String args)
A a = new A();
B b = new B();
A c = new B();
test i = new test();
i.func(a);
i.func(b);
i.func(new B());
answered 59 mins ago
Mohit Sharma
688
688
add a comment |Â
add a comment |Â
cceasy is a new contributor. Be nice, and check out our Code of Conduct.
cceasy is a new contributor. Be nice, and check out our Code of Conduct.
cceasy is a new contributor. Be nice, and check out our Code of Conduct.
cceasy is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52888360%2fjava-function-call-with-overload%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
See stackoverflow.com/questions/383947/…
– Raedwald
58 mins ago