overriding and overloading in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramadeviirrigireddy
    New Member
    • Jul 2007
    • 54

    overriding and overloading in java

    Can anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ramadeviirrigir eddy
    Can anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true?
    Have you read anything about generics yet?

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by ramadeviirrigir eddy
      Can anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true?
      Just try something like this:
      [CODE=java]
      public class Overriding {

      public void myFunction()
      {
      System.out.prin tln("I'm in the void.");
      }

      public int myFunction()
      {
      System.out.prin tln("I'm in the int.");
      return 0;
      }
      }
      [/CODE]It will tell you
      Code:
      Duplicate method myFunction() in type Overriding
      (or similar). So the answer is: No, you can't have several functions like that. To override a function, it MUST have different parameters. Then the type of the result doesn't matter anyway. So
      [CODE=java]
      public class Overriding {

      public static void main(String[] args)
      {
      myFunction();
      int i = (Integer) myFunction(3);
      }

      // This could be a void without the return line
      public static Object myFunction()
      {
      System.out.prin tln("I'm in the void.");
      return new Object();
      }

      // This could be "public static int myFunction(int i)" too
      public static Object myFunction(int i)
      {
      System.out.prin tln("I'm in the int.");
      return i;
      }
      }
      [/CODE]works fine, while it won't without the argument in the second function. What you can do is have a function, which takes and returns an int, one that takes and returns a double, etc. and call them the same. As long as the arguments are different, it'll work.
      Example:
      [CODE=java]
      public int myFunction(int i)
      {
      System.out.prin tln("I'm in the int.");
      return i;
      }

      public double myFunction(doub le d)
      {
      System.out.prin tln("I'm in the double.");
      return d;
      }
      [/CODE]
      Greetings,
      Nepomuk

      *Edit*
      Of course, when using Generics, you can do some wonderful things. Read about them. However, that's not exactly what you're looking for, if I understood correctly. The compiler or jvm doesn't find out the type itself.
      Last edited by Nepomuk; Sep 7 '07, 08:25 AM. Reason: The answer by r035198x

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You cannot talk about overriding when you have only one class.
        You need a method in a class with the same signature as a method in a super class of that class
        Last edited by r035198x; Sep 7 '07, 08:34 AM. Reason: the code tags bug had struck again

        Comment

        • ramadeviirrigireddy
          New Member
          • Jul 2007
          • 54

          #5
          Hi,

          The code you mentioned above is an example of overloading right. it is not overriding.i mean to say this code.

          [CODE=java]
          public int myFunction(int i)
          {
          System.out.prin tln("I'm in the int.");
          return i;
          }

          public double myFunction(doub le d)
          {
          System.out.prin tln("I'm in the double.");
          return d;
          }
          [/CODE]

          but overriding means you should have same method signature like the following code
          [CODE=java]
          class One
          {
          int a=10,b=20;

          public int add()
          {
          System.out.prin tln("a+b is:" + (a+b));
          return a+b;

          }

          }

          class Two extends One
          {
          int a=20,b=20;


          public int add()
          {
          System.out.prin tln("a+b is:" + (a+b));

          return a+b;
          }

          }

          public class Three {

          /**
          * @param args
          */
          public static void main(String[] args) {

          Two x = new Two();

          x.add();

          One y = new One();
          y.add();


          }

          }
          [/CODE]

          If you mention the return type as different in the above method then it'll give compilation error saying that "add() in Two cannot override add() in One; attempting to use incompatible return type" if u mention the return type in Two as double.
          Last edited by r035198x; Sep 7 '07, 08:44 AM. Reason: the code tags bug had struck again

          Comment

          • madhoriya22
            Contributor
            • Jul 2007
            • 251

            #6
            Originally posted by nepomuk
            Just try something like this:
            [CODE=java]
            public class Overriding {

            public void myFunction()
            {
            System.out.prin tln("I'm in the void.");
            }

            public int myFunction()
            {
            System.out.prin tln("I'm in the int.");
            return 0;
            }
            }
            [/CODE]It will tell you
            Code:
            Duplicate method myFunction() in type Overriding
            [CODE=java]
            public class Overriding {

            public static void main(String[] args)
            {
            myFunction();
            int i = (Integer) myFunction(3);
            }

            // This could be a void without the return line
            public static Object myFunction()
            {
            System.out.prin tln("I'm in the void.");
            return new Object();
            }

            // This could be "public static int myFunction(int i)" too
            public static Object myFunction(int i)
            {
            System.out.prin tln("I'm in the int.");
            return i;
            }
            }
            [/CODE][CODE=java]
            public int myFunction(int i)
            {
            System.out.prin tln("I'm in the int.");
            return i;
            }

            public double myFunction(doub le d)
            {
            System.out.prin tln("I'm in the double.");
            return d;
            }
            [/CODE]
            Greetings,
            Nepomuk
            Hi,
            I think you are talking about overloading here not overriding.
            Last edited by madhoriya22; Sep 7 '07, 08:39 AM. Reason: To add my signature :)

            Comment

            • ramadeviirrigireddy
              New Member
              • Jul 2007
              • 54

              #7
              Yes i too think that you are talking about overloading.
              Code:
              class One
              {
              int a=10,b=20;
              
              public int add()
              {
              System.out.println("a+b is:" + (a+b));
              return a+b;
              
              }
              
              }
              
              class Two extends One
              {
              int a=20,b=20;
              
              
              public double add()
              {
              System.out.println("a+b is:" + (a+b));
              
              return a+b;
              }
              
              }
              
              public class Three {
              
              	/**
              	 * @param args
              	 */
              	public static void main(String[] args) {
              		
              			Two x = new Two();
              
              			x.add();
              
              			One y = new One();
              			y.add();
              
              						
              	}
              
              }
              if you see the above code the class Two is returning double where as One is returning integer.

              if you compile this program it'll give error saying that "add() in Two cannot override add() in One; attempting to use incompatible return type"

              by this example can i say that "overriding method should have same return type also including the method signature."???

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Overloaded methods need to have different parameter lists (types and or number)
                while for overridden methods the parameter lists need to be assignable and the
                return type of the overridden method must be a supertype or identical to the
                return type of the overriding method; here's a small example:

                [code=java]
                import java.util.Colle ction;
                import java.util.List;

                class Base {

                public Collection func(int i) { return null; }

                public Object func(String s) { return null; }
                }

                public class Derived extends Base {

                public List func(int i) { return null; }

                public String func(String s) { return null; }
                }[/code]

                kind regards,

                Jos

                Comment

                • ramadeviirrigireddy
                  New Member
                  • Jul 2007
                  • 54

                  #9
                  Originally posted by JosAH
                  Overloaded methods need to have different parameter lists (types and or number)
                  while for overridden methods the parameter lists need to be assignable and the
                  return type of the overridden method must be a supertype or identical to the
                  return type of the overriding method; here's a small example:

                  [code=java]
                  import java.util.Colle ction;
                  import java.util.List;

                  class Base {

                  public Collection func(int i) { return null; }

                  public Object func(String s) { return null; }
                  }

                  public class Derived extends Base {

                  public List func(int i) { return null; }

                  public String func(String s) { return null; }
                  }[/code]

                  kind regards,

                  Jos
                  ok thanks a lot. now i understood the difference clearly.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    [CODE=java]class Foo {
                    public Number print() {
                    System.out.prin tln("Foo");
                    return 0;
                    }
                    }
                    class Bar extends Foo {
                    public Integer print() {
                    System.out.prin t("Bar");
                    return 0;
                    }
                    public static void main(String[] args) {
                    Foo foo = new Foo();
                    Bar bar = new Bar();
                    foo.print();
                    bar.print();
                    }
                    }[/CODE]

                    Y A T F E


                    Edit: and Jos beat me to the punch again

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      Originally posted by ramadeviirrigir eddy
                      ok thanks a lot. now i understood the difference clearly.
                      And I didn't even realize at the time, that I was making the Overloading/Overriding mistake. Shame on me. ^^
                      So, thanks to ramadeviirrigir eddy, madhoriya22 and JosAH for pointing out my mistake. ^^

                      Greetings,
                      Nepomuk

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by r035198x
                        Edit: and Jos beat me to the punch again
                        Haha!

                        kind regards,

                        Jos ;-)

                        Comment

                        • madhoriya22
                          Contributor
                          • Jul 2007
                          • 251

                          #13
                          Originally posted by r035198x
                          [CODE=java]class Foo {
                          public Number print() {
                          System.out.prin tln("Foo");
                          return 0;
                          }
                          }
                          class Bar extends Foo {
                          public Integer print() {
                          System.out.prin t("Bar");
                          return 0;
                          }
                          public static void main(String[] args) {
                          Foo foo = new Foo();
                          Bar bar = new Bar();
                          foo.print();
                          bar.print();
                          }
                          }[/CODE]

                          Y A T F E


                          Edit: and Jos beat me to the punch again
                          Hi,
                          I run ur example and got these two compile time errors ..
                          Code:
                          Foo.java:8: The method java.lang.Integer print() declared in class Bar cannot override the method of the same signature declared in class Foo. They must have the same return type.
                          	public Integer print() {
                          				 ^
                          Foo.java:4: Incompatible type for return. Can't convert int to java.lang.Number.
                          		return 0;
                          		^
                          2 errors
                          Are you trying to prove some point here or something else .. :)
                          Confused !
                          Last edited by madhoriya22; Sep 7 '07, 09:09 AM. Reason: To add my signature :)

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by madhoriya22
                            Hi,
                            I run ur example and got these two compile time errors ..
                            Code:
                            Foo.java:8: The method java.lang.Integer print() declared in class Bar cannot override the method of the same signature declared in class Foo. They must have the same return type.
                            	public Integer print() {
                            				 ^
                            Foo.java:4: Incompatible type for return. Can't convert int to java.lang.Number.
                            		return 0;
                            		^
                            2 errors
                            Are you trying to prove some point here or something else .. :)
                            Confused !
                            What's your compiler version?

                            Comment

                            • madhoriya22
                              Contributor
                              • Jul 2007
                              • 251

                              #15
                              Originally posted by r035198x
                              What's your compiler version?
                              Hi,
                              My compiler version is 1.5.0_04.
                              Note : I am not trying to prove anything wrong. Just trying to resolve my confusion :(
                              Last edited by madhoriya22; Sep 7 '07, 09:20 AM. Reason: To add my signature :)

                              Comment

                              Working...