This discussion is archived
1 2 Previous Next 22 Replies Latest reply: Nov 30, 2012 2:19 AM by 966867 RSS

Override generic parameters

966867 Newbie
Currently Being Moderated
Hello,

do you know a possibility to override methods with generic parameters. For example I've go a failure in class B because I use SortedMap instead of Map.

I understand that it is logically not possible to increase the restriction for parameter, you know any method how to do so.

public class A {



     public void setMap(Map<Integer, String> map) {



     }

}



public class B extends A {



     @Override

     public void setMap(SortedMap<Integer, String> map) {//failure here: can't override

          super.setMap(map);

     }

}
Thank you

Andrej
  • 1. Re: Override generic parameters
    jtahlborn Expert
    Currently Being Moderated
    your problem has nothing to do with generics. you can't change the parameters when you override a method (regardless of generics usage). the only thing you can do is make the return type more specific.

    if you want to force a narrower param type, you could do something like:
            @Override
         public void setMap(Map<Integer, String> map) {
              setMap((SortedMap<Integer,String>)map);
         }
         public void setMap(SortedMap<Integer, String> map) {
              super.setMap(map);
         }
  • 2. Re: Override generic parameters
    966867 Newbie
    Currently Being Moderated
    thank you for your answer,



    take a look at method above. I don't like this method because it has a lot of syntax, but it do what I want.


    public class C<M extends Map<Integer, String>> {
    
         public void setMap(M map) {
    
         }
    
    }
    
    
    
    public class D extends C<SortedMap<Integer, String>> {
    
         @Override
    
         public void setMap(SortedMap<Integer, String> map) {
    
              super.setMap(map);
    
         }
    
    }
    I hope some body would extend java to enable to do such stuff with less code and in more understandable syntax.

    Best Regards
    Andrej
  • 3. Re: Override generic parameters
    jtahlborn Expert
    Currently Being Moderated
    In your second scenario, there is no need to override the method in D, that's just redundant.
  • 4. Re: Override generic parameters
    966867 Newbie
    Currently Being Moderated
    Override marks the method as overridden and that is what I want.
    @Override annotation helpful is for type-safety and helps the compiler to find syntax-failures, else it has no meaning and is always redundant .

    Best Regards
    Andrej
  • 5. Re: Override generic parameters
    jtahlborn Expert
    Currently Being Moderated
    zarr wrote:
    Override marks the method as overridden and that is what I want.
    @Override annotation helpful is for type-safety and helps the compiler to find syntax-failures, else it has no meaning and is always redundant .
    i wasn't referring to the annotation, i was referring to the entire method, which is unnecessary.
  • 6. Re: Override generic parameters
    966867 Newbie
    Currently Being Moderated
    >
    i wasn't referring to the annotation, i was referring to the entire method, which is unnecessary.
    >

    The method in D uses SortedMap instead of Map and implements new logic. The advantage is the Map has not to be casted to SortedMap, thus the method in D can be used type-safely.
  • 7. Re: Override generic parameters
    EJP Guru
    Currently Being Moderated
    He is referring to the entire method with the @Override, as he said. The whole thing is redundant.
  • 8. Re: Override generic parameters
    966867 Newbie
    Currently Being Moderated
    please take a look at following example and explain why entire method is redundant?
    public class C<M extends Map<Integer, String>> {
         public void setMap(M map) {
     
         }
    }
    
    public class D extends C<SortedMap<Integer, String>> {
         @Override
         public void setMap(SortedMap<Integer, String> map)
              //new logic which is based on parameter and is type-safely
              SortedMap<Integer, String> headMap = map.headMap(10);
              super.setMap(headMap); 
         }
    }
    Best Regards
    Andrej
  • 9. Re: Override generic parameters
    Kayaman Guru
    Currently Being Moderated
    zarr wrote:
    please take a look at following example and explain why entire method is redundant?
    Well of course it's not redundant now, when you added logic. Now it's just badly designed.
  • 10. Re: Override generic parameters
    EJP Guru
    Currently Being Moderated
    please take a look at following example and explain why entire method is redundant?
    There is nothing to explain. As nobody has even seen this code until now, let alone commented on it in any way, your question is thoroughly asinine.
  • 11. Re: Override generic parameters
    966867 Newbie
    Currently Being Moderated
    Kayaman wrote:Well of course it's not redundant now, when you added logic. Now it's just badly designed.
    I personally don't use such design because the syntax is very complex, ussually I cast to the type I need. But the example shows the idea what I mean by my question. I want more type-safety for parameters in overridden methods.
    EJP wrote:There is nothing to explain. As nobody has even seen this code until now, let alone commented on it in any way, your question is thoroughly asinine.
    Please don’t bluster. I really don’t understand your point of view, I just try to understand the code I posted.
  • 12. Re: Override generic parameters
    966867 Newbie
    Currently Being Moderated
    Compared to Framework extension and variation points:
    1)     Modifying of return type is like variation point
    2)     Modifying of parameter type is like extension point
    What do you think about this idea?
  • 13. Re: Override generic parameters
    EJP Guru
    Currently Being Moderated
    I really don’t understand your point of view, I just try to understand the code I posted.
    My 'point of view' is simply that you can't rationally expect anybody to understand your code before you post it, and that asking people to explain statements they haven't made about code they haven't even seen is asinine. That's not 'bluster', it's just fact. If you want a better answer, ask a better question.
  • 14. Re: Override generic parameters
    EJP Guru
    Currently Being Moderated
    Compared to Framework extension and variation points:
    Whatever that might mean.
    1)     Modifying of return type is like variation point
    Modification of return types is called covariant return types.
    2)     Modifying of parameter type is like extension point
    Modification of parameters is called method overloading.
    What do you think about this idea?
    I don't think we need a new set of names for existing concepts.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points