Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Issue (probably me misunderstanding) with using covariants.

    The Classes I will spilt in hopes it will be easier to read.

    Java Code:
    public class A { 
     //The instance Variables for para1 & para2
    public  A (para1, para2 ) { 
        // construct 
    }
    
       //some more methods
    }
    
    public class B extends A { 
    
    public  B (para1, para2) { 
        super (para1,para2) 
    }
        //methods
    }
    Java Code:
    public class C {
    
    private A something;  
    
    public C (para1 para2) { 
        something = new A (para1, para2); 
     }  
    
    public getA () { 
        return something; 
    }
    }
    Java Code:
     
    public class D { 
    
    private C [] newArray; 
    
    public D (size) { 
        newArray = new C [size]; 
    } 
    
    public A getA (key) { 
       return  newArray [key].getA(); 
    }
    
    public Class E extends D { 
    
    public E { super (size) } 
    
    public B Getb (key) { 
    // The part I am unsure about. 
    return super.getA (key); 
    }
    So my problem is when type B [in the main] is declared it is suppose to be stored Class C as the variable 'something'. Then Class C can be stored in Class D in the array newArray. However, in Class E I would like it so the type returned is B. Ultimately, I don't get why this wouldn't work as I thought covariants allowed you to return the decendent of a class? I guess my end goal is to create an object of type B then return B in Class E.

    Now here is too hoping that what I actually typed makes sense to someone.
    Last edited by pipsy17; 10-13-2014 at 07:16 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Is that supposed to be Java code?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by DarrylBurke View Post
    Is that supposed to be Java code?
    Yes and no. I realize this particular set of 'code' would not compile and should be more interpreted as psuedo then I guess java. However, I was attempting to rid of superfluous information that isn't relevent to the issue. That being said the actual program in question is written java and complies on exception to the one isssue mentioned and do hope for an answer in java or at least in psuedo so I can understand where I am going wrong.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Issue (probably me misunderstanding) with using covariants.

    It doesn't help to post code that ignores the fact that Java is case sensitive.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by DarrylBurke View Post
    It doesn't help to post code that ignores the fact that Java is case sensitive.
    Fair enough, but, I think I have clarified my problem clearly enough and if not please feel free to be direct about what isn't clear about it and I'd be happy to clarify.
    Last edited by pipsy17; 10-13-2014 at 05:46 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by pipsy17 View Post
    Fair enough, but, I think I have clarified my problem clearly enough
    You think this makes sense?
    Java Code:
    public Class E extends D { 
     
    public E { super (key) } 
     
    public B Getb (key) { 
    // The part I am unsure about. 
    super.getA (key); 
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    okay that one is a little off... because the 'key' variable in the constuctor should be size. Otherwise, the second method is saying return type B of whatever is returned by super.getA and getA in this class essentially grabs the variable 'something' as specified by Class C. However, the complier (rightly so) complains that the variable 'something' is of type A despite it declared as a type B in the main as indicated by me in the text below. Thus my issue with covariants.
    Last edited by pipsy17; 10-13-2014 at 07:29 AM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by pipsy17 View Post
    okay that one is a little off... because the 'key' variable in the constuctor should be size.
    What constructor?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by pipsy17 View Post
    However, the complier (rightly so) complains that the variable 'something' is of type A despite it declared as a type B in the main as indicated by me in the text below.
    Focussing on this one issue, GetB is expecting to return an instance of B but GetA is returning an instance of the superclass. Although the instance returned is actually of type B, until you tell it otherwise it will only be seen as type A. Within your method you can return an instance of the superclass (If there was one) but you cannot return an instance of a subclass without first casting it.

    See the following articles for more information.
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    regards.

  10. #10
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by Ronin View Post
    Focussing on this one issue, GetB is expecting to return an instance of B but GetA is returning an instance of the superclass. Although the instance returned is actually of type B, until you tell it otherwise it will only be seen as type A. Within your method you can return an instance of the superclass (If there was one) but you cannot return an instance of a subclass without first casting it.

    See the following articles for more information.
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    regards.

    Ahh I see so I should be returning
    Java Code:
     return (B) super.GetA (key);
    because the complier explictly needs be told. Thanks!

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Are you certain about this? I looked at the code and did not see in the sequence of things
    where returning type A could be cast to B (since B actually extends A). In this case, the instance
    returned was actually type A and had nothing to do with B. Therefore it looks like it would
    through a ClassCastException. However, the code was somewhat difficult to follow.

    Regards,
    Jim
    Last edited by jim829; 10-14-2014 at 06:02 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by jim829 View Post
    Are you certain about this? I looked at the code and did not see in the sequence of things
    where returning type A could be cast to B (since B actually extends A). In this case, the instance
    returned was actually type A and had nothing to do with B. Therefore it looks like it would
    through a ClassCastException. However, the code was somewhat difficult to follow.

    Regards,
    Jim
    I think a ClassCastException would be thrown if I actually did this
    Java Code:
    public B Getb (key) {
    return super.getA (key);
    }
    but when I do this (if I am understanding the issue correctly now)

    Java Code:
    public B Getb (key) {
    return (B) super.getA (key);
    }
    It should be allowable well... to be honest I just looked up ClassCaseException to look at the javadocs and I found the answer to my question albeit in a indirect manner...

    exception - Can someone explain "ClassCastException" in Java? - Stack Overflow

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Say B extends A.

    For (B) super.getA(key) where A = new B(), then you won't get a class cast exception.
    For (B) super.getA(key) where A = new A(), then you will get a class cast exception.

    It depends on the nature of the return of getA(key) whether A is an instance of A or an instance of B.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by jim829 View Post
    Say B extends A.

    For (B) super.getA(key) where A = new B(), then you won't get a class cast exception.
    For (B) super.getA(key) where A = new A(), then you will get a class cast exception.

    It depends on the nature of the return of getA(key) whether A is an instance of A or an instance of B.

    Regards,
    Jim
    getA(key) should be returning an instance of B.

  15. #15
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by jim829 View Post
    Are you certain about this?
    I hang my head in shame at such a mistake. I got so hung up on the "declaration of B in the main" I completely missed the initial declaration of something as type A in C. I'm blaming a sleepless night and the early morning.

    Regards.
    Last edited by Ronin; 10-14-2014 at 07:16 PM. Reason: Typo

  16. #16
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by Ronin View Post
    I hang my head in shame at such a mistake. I got so hung up on the "declaration of B in the main" I completely missed the initial declaration of something as type A in C. I'm blaming a sleepless night and the early morning.

    Regards.
    Haha no worries I appreciate the help.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by pipsy17 View Post
    getA(key) should be returning an instance of B.
    How could it? No where that I can see do you create an instance of B. Everything
    in your code is about returning class A.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Either something should be declared as a new instance of B or you could construct B outside of this class and pass this in as a parameter. Either way as 'something' is declared as type A it will need to be cast to be see as B. Alternatively, 'something' could also be defined as B.

    On a side not I have to ask if class C is doing anything other than just holding the instance of A?

    Regards.

  19. #19
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by jim829 View Post
    How could it? No where that I can see do you create an instance of B. Everything
    in your code is about returning class A.

    Regards,
    Jim
    No your right what is wrong is my code. In class C It shouldn't be
    Java Code:
     something = new A (para1, para2)
    but rather
    Java Code:
     something = b
    Where b is instance of object B and obviously that Constructor should use a more appropriate parameter.

  20. #20
    pipsy17 is offline Member
    Join Date
    Oct 2014
    Posts
    10
    Rep Power
    0

    Default Re: Issue (probably me misunderstanding) with using covariants.

    Quote Originally Posted by Ronin View Post
    On a side not I have to ask if class C is doing anything other than just holding the instance of A?

    Regards.
    It does do other stuff but I just regarded the other stuff as irrelevant to this problem.

Page 1 of 2 12 LastLast

Similar Threads

  1. misunderstanding the dot operator
    By willemjav in forum New To Java
    Replies: 9
    Last Post: 07-06-2013, 09:51 PM
  2. probably a principle misunderstanding of OOP?
    By willemjav in forum AWT / Swing
    Replies: 8
    Last Post: 04-05-2013, 12:57 AM
  3. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  4. Same issue as before....
    By hayden06f4i in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 01:49 AM
  5. Misunderstanding of Break
    By Steve Whalen in forum New To Java
    Replies: 6
    Last Post: 10-22-2008, 06:31 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
  •