Results 1 to 5 of 5
  1. #1
    davis123877 is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Covariant Return in Overridden Method

    For starters here is my code:

    Java Code:
    class A {
        int x=5;
    }
    class B extends A {
        int x=6;
    }
    public class CovariantTest {
        public A getObject() {
            System.out.println("super");
            return new A();
        }
        public static void main(String[] args) {
            CovariantTest c1 = new SubCovariantTest();
            System.out.println(c1.getObject().x);
        }
    }
    class SubCovariantTest extends CovariantTest {
        public B getObject() {
            System.out.println("sub");
            return new B();
            
        }
    }
    and this is the output I get:
    sub
    5

    I am unable to figure out how this is outputting 5 instead of 6. The getObject method of SubCovariantTest is obviously the one being called, and it returns a new B(). So why am I getting class A's x value? I thought since I was getting a B object returned that I would get B's x value. I'm so confused! Please help this is driving me crazy!
    Last edited by Tolls; 03-05-2015 at 02:10 PM. Reason: Add code tags

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Covariant Return in Overridden Method

    Brrrrr, this is so horrible that I can only assume it came from a multiple choice test.

    Did you also try this?

    Java Code:
    public static void main(String[] args) {
    SubCovariantTest c1 = new SubCovariantTest(); // changed variable type to the subclass
    System.out.println(c1.getObject().x);
    }
    Then "all of a sudden" the code prints the 6 you expect to see. Perhaps that gives you a hint as to what might be going on?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Covariant Return in Overridden Method

    You can't override fields like that and expect them to replace the one in the super class. The runtime is picking the one from its superclass. If you cast the result to B it picks "x" from B and does show the corect value:
    Java Code:
    System.out.println( ((B)c1.getObject()).x);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Covariant Return in Overridden Method

    At least I was giving the OP the chance to have that "AHA!" moment of figuring it out himself :/

    Oh well, as long as it becomes clear I guess.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Covariant Return in Overridden Method

    I hope my comment gave him the AHA moment you were looking for. I can imagine for a new Java coder, and given the shitty code example, that is (supposedly) overriding fields and also the confusingly overridden method getObject() with different return types can be a mystery.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 02:15 PM
  2. Why a static method cannot be overridden?
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-17-2013, 01:58 PM
  3. Subclass Overridden Method throws Exception Rule?
    By hemanthjava in forum New To Java
    Replies: 5
    Last Post: 05-14-2010, 02:20 PM
  4. covariant return types
    By coder in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 09:16 AM
  5. Invoking a superclass version of a overridden method
    By CyberFrog in forum New To Java
    Replies: 3
    Last Post: 05-25-2009, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •