This discussion is archived
4 Replies Latest reply: Aug 12, 2010 6:44 AM by 843793 RSS

Overriding getter of generic field?

843793 Newbie
Currently Being Moderated
Hi all! Sorry if this has been asked before, my google-fu failed.

If I have an abstract class with a generic assigned, and includes a protected generic field, then I override this class and provide a specific type for the generic, when I override the getter originally assigned in the abstract class, I get an incompatible type error. Below is an example of the problem I am having:
public abstract class Parent<F> {

     F field;

     public F getField() {
          return this.field;
     }
}

public class Child extends Parent<Integer> {

     @Override
     public Integer getField() {
          //do something
          this.field = new Integer(10);
          return this.field;     // <---- the error is here
     }
}
The return statement in the overridden class has an incompatible type error. If alternatively, I replace that line with this:
return (Integer)this.field;
the problem goes away. I can do this, of course, but I'm confused as to why I have to. Doesn't assigning the generic a specific class eliminate the need for the casting (in principle)? Any help is appreciated, thanks!
  • 1. Re: Overriding getter of generic field?
    DrClap Expert
    Currently Being Moderated
    I thought that the "incompatible type" error message also went on to describe what was incompatible about the types. Does it not?
  • 2. Re: Overriding getter of generic field?
    843793 Newbie
    Currently Being Moderated
    Hello nyundi,

    if that's your actual code then it should compile.

    With kind regards
    Ben
  • 3. Re: Overriding getter of generic field?
    EJP Guru
    Currently Being Moderated
    If that's your actual code, it does compile.

    So what does the real code look like?
  • 4. Re: Overriding getter of generic field?
    843793 Newbie
    Currently Being Moderated
    Hi all, thanks for taking a look! You are right, this isn't the real code, I tried to boil it down to make it more postable but failed! In any event, I did find the solution, but for anyone in the future with this problem, I will post an actual bit of code that fails and the solution (where I went wrong)! Thanks again.

    The following code shows what I was trying to accomplish, and actually does not work;
    public class TestGenerics {
    
        private abstract class GrandParent<F> {
            protected F field;
            
            public F getField() {
                return this.field;
            }
        }
        
        private abstract class Parent<A> extends GrandParent {
            
        }
        
        private abstract class Child extends Parent<Integer> {
            @Override
            public Integer getField() {
                return this.field;  //<----- the error is here
            }
        }
        
    }
    To fix this problem, I had to also map the generic inherited by Parent from GrandParent to the same variable name:
    public class TestGenerics {
    
        private abstract class GrandParent<F> {
            protected F field;
            
            public F getField() {
                return this.field;
            }
        }
        
        private abstract class Parent<A> extends GrandParent<A> {   //<---- the <A> after GrandParent was the key; this allows the generic type to trickle down to Child, who can then define it
            
        }
        
        private abstract class Child extends Parent<Integer> {
            @Override
            public Integer getField() {
                return this.field;
            }
        }
        
    }
    Hopefully this will help someone else out in the future who is stuck with a similar problem! :)