Java Function Call with Overload

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question









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














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.










share|improve this question









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












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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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
















  • 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












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.






share|improve this answer





























    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





    share|improve this answer



























      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





      share|improve this answer



























        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());






        share|improve this answer




















          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          cceasy is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          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






























          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.






          share|improve this answer


























            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.






            share|improve this answer
























              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.






              share|improve this answer














              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 58 mins ago

























              answered 1 hour ago









              khelwood

              28.6k63858




              28.6k63858






















                  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





                  share|improve this answer
























                    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





                    share|improve this answer






















                      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





                      share|improve this answer












                      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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 1 hour ago









                      SBylemans

                      589216




                      589216




















                          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





                          share|improve this answer
























                            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





                            share|improve this answer






















                              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





                              share|improve this answer












                              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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 11 mins ago









                              Oleksandr

                              7,34533366




                              7,34533366




















                                  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());






                                  share|improve this answer
























                                    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());






                                    share|improve this answer






















                                      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());






                                      share|improve this answer












                                      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());







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 59 mins ago









                                      Mohit Sharma

                                      688




                                      688




















                                          cceasy is a new contributor. Be nice, and check out our Code of Conduct.









                                           

                                          draft saved


                                          draft discarded


















                                          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.













                                           


                                          draft saved


                                          draft discarded














                                          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













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          List of Gilmore Girls characters

                                          Confectionery