1 2 Previous Next 20 Replies Latest reply: Sep 11, 2009 12:06 AM by 782681 RSS

    Is Overloading possible in Sub class

    so*797109*as
      In every book I found that Overloading means method with the same name and with different parameter define in same class.
      But is possible if I define method with the same name and with different parameter in Sub class?

      Like example given below :

      public class OverLadingTest {

           public void test(int a, int b) {
                System.out.println("Inside test(int a, int b)");
           }

           public static void main(String args[]){
                     OverloadedSubclass overloadedSubclass = new OverloadedSubclass();
                     overloadedSubclass.test(10.20,10.20);
           }

      }

      class OverloadedSubclass extends OverLadingTest {
           public void test(double a, double b) {
                System.out.println("Inside test(double a, double b)");
           }


      }


      Thanks in advance.
        • 1. Re: Is Overloading possible in Sub class
          EJP
          Did you try it?
          • 2. Re: Is Overloading possible in Sub class
            843789
            What?

            And have the universe sucked into a quantum singularity. Or am I thinking of the Hadron collider?
            • 3. Re: Is Overloading possible in Sub class
              EJP
              I know.

              I just have this strange concept that javac gives you the correct answer, by definition, practically instantaneously, and locally, whereas the Internet may not give you an answer at all, let alone a correct answer.

              Let alone the answer you want to hear ;-)
              • 4. Re: Is Overloading possible in Sub class
                843789
                I have no need for a class named Sub...
                • 5. Re: Is Overloading possible in Sub class
                  800308
                  The nays have it!
                  package forums;
                  
                  class Singularity
                  {
                    public void test(int a) {
                      System.out.println("Singularity.test(int a)");
                    }
                  }
                  
                  class Universe extends Singularity
                  {
                    @Override
                    public void test(int a) {
                      System.out.println("Universe.test(int a)");
                    }
                  
                    public void test(int a, int b) {
                      System.out.println("Universe.test(int a, int b)");
                    }
                  }
                  
                  public class OverloadingTest
                  {
                    public static void main(String args[]) {
                      Singularity s = new Universe();
                      s.test(10);
                      s.test(10, 20);
                    }
                  }
                  compile
                  C:\Java\home\src\forums>"C:\Program Files\Java\jdk1.6.0_12\bin\javac.exe" -Xlint -d C:\Java\home\classes -cp c:\java\home\src; C:\Java\home\src\forums\OverloadingTest.java 
                  C:\Java\home\src\forums\OverloadingTest.java:27: test(int) in forums.Singularity cannot be applied to (int,int)
                      s.test(10, 20);
                       ^
                  1 error
                  I'd say that's pretty conclusive, wouldn't you? Go read up on Java's definition of a [method signature|http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html]... It's not just the name, putz!

                  Cheers. Keith.
                  • 6. Re: Is Overloading possible in Sub class
                    EJP
                    The nays have it!
                    No they don't. The method compiled. So you can do it.

                    But its new signature hides the previous signature in the base class, so the call to the base class method didn't compile.
                    • 7. Re: Is Overloading possible in Sub class
                      so*797109*as
                      My question was very simple. If I used same name but different parameter for a method in Subclass then will it be a overloaded method.

                      Is test method in Universe class overloaded.

                      class Singularity {
                           public void test(int a) {
                                System.out.println("Singularity.test(int a)");
                           }
                           
                      }

                      class Universe extends Singularity {
                           
                           public void test(int a, int b) {
                                System.out.println("Universe.test(int a, int b)");
                           }
                      }

                      public class OverloadingTest {
                           public static void main(String args[]) {
                                Universe s = new Universe();
                                s.test(10);
                                s.test(10, 20);
                           }
                      }
                      • 8. Re: Is Overloading possible in Sub class
                        843789
                        WOW!

                        Two months and you still haven't been able to work this out for yourself.
                        • 9. Re: Is Overloading possible in Sub class
                          796764
                          No. The signature of a method is its NAME and its PARAMETER.
                          void test(int a)
                          void test(int a, int b)
                          void test(int a, double b)
                          ...

                          Those above methods are all different; therefore, there is no overloading method in your case.
                          • 10. Re: Is Overloading possible in Sub class
                            EJP
                            BZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZT

                            There is no overriding method. There are several overloaded methods.

                            Do try to get it right when you post please.
                            • 11. Re: Is Overloading possible in Sub class
                              843789
                              I. //pretty sure
                              Kirax wrote:
                              No. The signature of a method is its NAME and its PARAMETER.
                              /cough/ return type /cough/

                              II. // not 100% sure
                              @OP:
                              A Universe is different a Singularity and would hence do different, horrible things with a and b:
                              class Singularity{
                              public void test(int a, int b){throwAwayFromEachOther(a, b);}// Universes expand
                              }
                              class Universe extends Singularity{
                              public void test(int a, int b){slamTogether(a, b);}// Singularities contract
                              }
                              So, though you give the 2 methods the same input (int a, int b) and get back a void(==nothing) in both cases, the both do oppositionally different stuff to the input. The possibility to do this(so I believe) is called overloading. If its not, Im waiting to learn from this thread, too.

                              Edit: Was it overriding? If so, well, Bzzzt to me, too.

                              Edited by: anotherAikman on Sep 10, 2009 12:45 AM
                              • 12. Re: Is Overloading possible in Sub class
                                EJP
                                Why all the confusion? It's in the Java Language Specification. These terms have been used consistently the same way for over twenty years.

                                Overriding
                                Overloading
                                • 13. Re: Is Overloading possible in Sub class
                                  796764
                                  My bad. I was thinking about overriding.

                                  @anotherAikman :
                                  I'm pretty sure that the return type does not affect the method signature. If you try to declare 2 methods:
                                  void test(){}
                                  int test(){ return 0; }
                                  you will get a compile error.
                                  • 14. return type / method signature
                                    782681
                                    Kirax wrote:
                                    My bad. I was thinking about overriding.

                                    @anotherAikman :
                                    I'm pretty sure that the return type does not affect the method signature. If you try to declare 2 methods:
                                    void test(){}
                                    int test(){ return 0; }
                                    you will get a compile error.
                                    what does it mean "return type does not affect the method signature"?

                                    // side note since Java 5 it is in some cases allowed to override method using different return type ([covariant returns|http://www.google.com/search?q=java+covariant+return+types|google])

                                    Edited by: gnat on Sep 10, 2009 10:59 AM
                                    1 2 Previous Next