3 Replies Latest reply: Feb 12, 2010 10:18 AM by 843793 RSS

    Override of abstract generic method!

    843793
      Hi I have this problem:
      Generic method in abstract class:

      abstract public class AbstractRule {

      // method

      protected abstract <T> T getvalue();

      protected abstract <T> void updateValue(T value);

      }


      And then let me say , we want to extend this class by adding one filed suppose integer :

      public class ExpireLogin extends AbstractRule {

      private Integer numberOfLogins;

      @Override
      public Integer getvalue() {
           return numberOfLogins;
      }


           
      @Override
           public void updateValue(Integer value) {
                          numberOfLogins=value;
      }


      }


      the first method shows :

      - Type safety: The return type Integer for getvalue() from the type ExpireLogin needs unchecked conversion to
           conform to T from the type AbstractRule
      - implements de.dtnet.swdcore.customer.persistence.domain.AbstractRule.getvalue


      the second have a compile error:

      - T cannot be resolved to a type
      - The method updateValue(T) of type ExpireDaysFromFirstLogin must override or implement a supertype
           method


      if we change the methods to:


      @Override
      public <Integer> Integer getvalue() {
           return durationInDays;
      }
           
      @Override
           public <Integer> void updateValue(Integer incValue) {
                durationInDays = incValue;     
      }

      the bought are having compile errors

      Type mismatch: cannot convert from Integer to java.lang.Integer

      Is this possible to be done at all?

      I dont like to add a generic to the hole abstract class,
      because this will make all to have diferent compile time abstract class, witch i dont like to gain..

      Any help would be nice..
        • 1. Re: Override of abstract generic method!
          843793
          I think making AbstractRule generic over a type is your best bet.
          public abstract class AbstractRule<T> {
              protected abstract T getValue();
              protected abstract void updateValue(T newValue);
          }
          
          public class ExpireLogin extends AbstractRule<Integer> {
              @Override protected Integer getValue() { /* ... */ }
              @Override protected void updateValue(Integer newValue) { /* ... */ }
          }
          Or, if you'd really rather not introduce the type parameter on the class, have AbstractRule deal in Objects:
          public abstract class AbstractRule {
              protected abstract Object getValue();
              protected abstract void updateValue(Object newValue);
          }
          
          public class ExpireLogin extends AbstractRule {
              @Override protected Integer getValue() { /* covariant return type OK */ }
              @Override protected void updateValue(Object newValue) { /* runtime type-check the parm */ }
          }
          What's your concern with introducing the type parameter on AbstractRule?
          • 2. Re: Override of abstract generic method!
            843793
            Hi,

            First tnx for the replay!

            I already use the second solution..i was and still am, just curies :)

            public abstract class AbstractRule {
            protected abstract Object getValue();
            protected abstract void updateValue(Object newValue);
            }

            public class ExpireLogin extends AbstractRule {
            @Override protected Integer getValue() { /* covariant return type OK */ }
            @Override protected void updateValue(Object newValue) { /* runtime type-check the parm */ }
            }

            witch is logical...


            Makinng the AbstractRule generic over a type...its not a good solution..

            Let me say I use that and i want now to create a new rule..

            public class DateRule extends AbstractRule<Date> {
            @Override protected Date getValue() { /* covariant return type OK */ }
            @Override protected void updateValue(Date newValue) { /* runtime type-check the parm */ }
            }


            so the compile time generic of the DateRule and the ExpireLogin would be diff...
            the idea of the super abstract class is to be able to work with the bought classes in same time...

            Let me say i want to create a list holding the bought classes...

            List<AbstractRule<from what?!>> list = new ArrayList<AbstractRule<from what?!>>(); // not posibble to create this kind of list :)
            I hope i gave a good exp..

            But any way..i can use row tipe of AbstractRule but then it would be treated just as an object...
            have to cast it all the time ( this is funny, why we implement the generics :) )

            but seems to me..this is not possible to be done...

            We can have a similar case like this with interface..

            Let me say we have an interface:

            public interface Rule{

            // we have methods where we dont like to have generics...



            }
            • 3. Re: Override of abstract generic method!
              843793
              continuing...press send by exc :)

              public interface Rule{

              // we have methods where we dont like to have generics...
              public is valid (HelperObject somehelperObject);

              public void calculateExpensivnes(HelperObject somehelperObject);

              etc...
              // and now to methos like get value and set value

              public <T> T getValue();

              public <T> setValue(T value);

              }

              there is no possibility this 2 methods to be implemented..thats how seems to me...


              So it comes out we can use generic methods in non global generic classes, just as utils...