How to override a method with different return type

Discussion in 'Java' started by sandy, Nov 15, 2005.

  1. sandy

    sandy Guest

    hi all,
    To begin with i thank the creaters of this group.
    I am new java world and facing a new challenge.
    i have a method in class A named
    protected int foo(int args) { /* ..... */ }
    and have a class B that extends class A and would like to have a
    method foo in it which overrides it as
    public double foo(int args) { /*........*/}
    i need something like above code, i.e the return type be different ,
    can anybody help me on this

    thanks
    sandy
     
    sandy, Nov 15, 2005
    #1
    1. Advertising

  2. sandy

    J. Verdrengh Guest

    > i have a method in class A named
    > protected int foo(int args) { /* ..... */ }
    > and have a class B that extends class A and would like to have a
    > method foo in it which overrides it as
    > public double foo(int args) { /*........*/}


    This is not allowed in Java. And if it were allowed, the return type of the
    overriding method (double in your case) should be 'more specific' then the
    return type of the overriden method (int in your case). After all a subclass
    should obey the contract of its superclass. So if the return type of the
    overriden method is double, it would be legal (from a object-oriƫnted
    programming point-of-view) to override it with a method returning a int
    (since each int can be implicitly casted to a double). But again: none of
    this is possible in Java.
     
    J. Verdrengh, Nov 15, 2005
    #2
    1. Advertising

  3. sandy

    sks Guest

    "J. Verdrengh" <> wrote in message
    news:4379dbf8$0$3637$...
    >> i have a method in class A named
    >> protected int foo(int args) { /* ..... */ }
    >> and have a class B that extends class A and would like to have a
    >> method foo in it which overrides it as
    >> public double foo(int args) { /*........*/}

    >
    > This is not allowed in Java. And if it were allowed, the return type of
    > the overriding method (double in your case) should be 'more specific' then
    > the return type of the overriden method (int in your case). After all a
    > subclass should obey the contract of its superclass. So if the return type
    > of the overriden method is double, it would be legal (from a
    > object-oriƫnted programming point-of-view) to override it with a method
    > returning a int (since each int can be implicitly casted to a double). But
    > again: none of this is possible in Java.


    You can do that with Object types (not primitives) in 1.5
     
    sks, Nov 15, 2005
    #3
  4. sandy

    Roedy Green Guest

    On Tue, 15 Nov 2005 13:21:01 -0000, "sks" <> wrote,
    quoted or indirectly quoted someone who said :

    >You can do that with Object types (not primitives) in 1.5

    and the overriding type has to be more specific, e.g. base returns
    Dog, override always returns Dalmatian, not the reverse. Dalmatian
    is still a Dog.

    You can't break the contract of the original base class.
    ..
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lucas
    Replies:
    5
    Views:
    958
    Chris Uppal
    Apr 12, 2004
  2. Pradeep Kumar
    Replies:
    29
    Views:
    1,060
    Dale King
    Apr 15, 2006
  3. David White
    Replies:
    12
    Views:
    2,625
    David Sobey
    Apr 1, 2005
  4. Replies:
    2
    Views:
    527
    vahan
    Jul 7, 2006
  5. Donn Ingle
    Replies:
    0
    Views:
    313
    Donn Ingle
    Nov 12, 2007
Loading...

Share This Page